Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1131853lqd; Thu, 25 Apr 2024 07:00:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWZV9foiB6YIv+lroHQxRyzZOkod26aGR/v65MTCU+W/5ZoIoSG3TfUIfwVdkYOcNBRMwqluUu5g1W66+Nd3rM6ZrxRvaZtROvD3TvFTw== X-Google-Smtp-Source: AGHT+IHDx7NkM0ooIkBVkLbbjcdGOJiGggL3MBZx8HZSgPpOc4BGEuJ5ByiToCB4T4l84jm0C3X5 X-Received: by 2002:a05:6402:2032:b0:572:2f0f:ba31 with SMTP id ay18-20020a056402203200b005722f0fba31mr4807073edb.1.1714053608910; Thu, 25 Apr 2024 07:00:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714053608; cv=pass; d=google.com; s=arc-20160816; b=L1nsj31NqROZuMzQrU2NBjLH8Ka2VBcFcYCZHmsPC9bsyPQZNpx5auBvpPkEH5IIrF pvTC6EEKTQXbKHufmdTzIgxJGRRKyvzvdSXOVRupf7fmweIb4i817VgOIZYMC1c+yqyt m0Y6U8bfS4M+yASbKfIF9LvkxJc/rS0t6yDib/Wuh/iOP76zHWBP+Eiv6GCOe/M5vqb4 fYGADGHLOOVOGfIhj10Abu3cLJ5YFRixwFX+ME028eJacPIfPq4J3tjTzHywlbn1GIjx jKL7fI5ae16YtlMdn3Lrehvorxmlbws6g3lPOT6y6n7KXA38o8OWKdd+e+wFGtXLnOiD 1W1Q== 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=StNx8DU1j4LejZqbdm7e8k32hyosnMnsfQH7iCE7APg=; fh=Cxwo/A+C89tjOzVq8R/o/DH+l+P6Lpt8VwbqwppZI0Y=; b=oKBc5Iely8f52F3cwURAEH7Fsvw28J4PWQrtof1c+PS2BVa18tM4Sij0HS2eA0aeYu xgwDGAZmrVXPEMENqn78RAFuoNrhJ6yoMOUD65v/efl+0X4FvgZsOKYH8y4lWou8vuSr TCuOKhg81ryCSbfPXtn7yG0wPNvFs1PhuCpUBYKdj5EbgYWTb6oGk2IT8o4NaZA7ys0S zssajoNN4MnyS+QciClbACY2HLok12buQwtoMnk/Fw8XSLDsuxJoKqIfTPdOAmXFh9Zz VLG9Y2BBuLQDGU9lZ6w5XTfGKlwZ+5EozJsgJNvAo/9bA8wWNbKrWjfQu4lDjsqT9FIT cG0g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HNE5RsAg; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-158639-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158639-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. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id r19-20020aa7d153000000b0057222c8e624si3639447edo.398.2024.04.25.07.00.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 07:00:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-158639-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HNE5RsAg; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-158639-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158639-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 A40991F221F3 for ; Thu, 25 Apr 2024 14:00:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4565C149DE9; Thu, 25 Apr 2024 13:59:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HNE5RsAg" 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 6646C14A0AD; Thu, 25 Apr 2024 13:59:30 +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=1714053570; cv=none; b=UHWUskXIeeeT/s/e2WMUotcYORq+qKq9WNlhCPEuUn1HLKRIJyc3OmRcsAS5ktpQL9I/KLCT7gnYDxIg8MFggtH1if1bfnFgzVCbx8gMerDWxwU6BUCcVPJ/yia8illxVvIEpk7PCpIRtksudADbDh+1ehOKUIF/EFcqKizfk2c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714053570; c=relaxed/simple; bh=/jfDurkkyTX1CjxTQWI9/U9pueeszadMVJLu6GiEwj0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CQBYUo6bJbXE6eQuMEX2jmxrofo7u9ogMPgii1CReUGbo6xVdA5o4Puh1sVehMqOmmeXImimULRehvh6sMbhMHlJJN0ao5ixs6A9bLywAAhcATTp47bAVSNYHfOuXFUOBVN2c9peBTHbUIBsGBqeBWQeCwEG0Lml7e4TGQHRU5w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HNE5RsAg; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA319C3277B; Thu, 25 Apr 2024 13:59:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714053570; bh=/jfDurkkyTX1CjxTQWI9/U9pueeszadMVJLu6GiEwj0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=HNE5RsAgTaZoxrIvlG1EZ6FNVeBiolN9FzELHstT8/YZC48DrHK8K1+Z2hi5wlfjB IM8VM/RB4xTlPAfw0BetGUQ3zZewCaIPD2SZcNtgBC2fCnHIlc7GOxaiK7oua15rJb vp2k7f4kWXcVWMLkVNIFnc6ilydDxJxicpUbd/9nyL/mu9TIDV6MB0u/BeKB6jR3Jz FxO6qS2LBhun3RpAuDHASQfDrEbEendMRGjHw6kHuruOeMZ46rlfp3IxtXKnVXP/CJ JW/9GtEK/3SylZATP/21S1nSKenKfpo+1F8HvzCMxnl67Nwu213DntyRBu7WbReUpM mgGaNCNQRF0Ww== From: Benjamin Tissoires Date: Thu, 25 Apr 2024 15:59:13 +0200 Subject: [PATCH 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: <20240425-bpf-next-v1-2-1d8330e6c643@kernel.org> References: <20240425-bpf-next-v1-0-1d8330e6c643@kernel.org> In-Reply-To: <20240425-bpf-next-v1-0-1d8330e6c643@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=1714053558; l=2311; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=/jfDurkkyTX1CjxTQWI9/U9pueeszadMVJLu6GiEwj0=; b=Mz0bcCjh5PHLLrFEt0aDcjPvdnjN285NxBvdb63Pfw6n7lah+bC7DmxGNCp7JMh4WAhpXyb1x UrYXaq5DWWZB4TGvBQtqBxpb+smpQMVI3NtB3RmAWaay6gpNWQu/nmE 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 --- 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); - } } /* Called when map->refcnt goes to zero, either from workqueue or from syscall */ -- 2.44.0