Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6263428imu; Mon, 21 Jan 2019 06:04:00 -0800 (PST) X-Google-Smtp-Source: ALg8bN5fq9ESnygHnmlpLN6VynaJR5NA5Oiw7Rd5tWdBDdFTMIQl7mCUVQQwvB8GRJPGYroj+L2q X-Received: by 2002:a62:a209:: with SMTP id m9mr30287772pff.218.1548079440302; Mon, 21 Jan 2019 06:04:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548079440; cv=none; d=google.com; s=arc-20160816; b=jdzHsOAxm2pq3IutCMJhhW3W6tEV3QZygDl0AZhrIE+YA2rKUsQVYqLJLLw1OxdYh3 M1UVLwd/lZv1nbequnD7HhXHkxgNgqIyDgQ5jv4atYbrIYL+lF7lOnKvM1ZD+/VtG/7r EoKliuodIresr/w9subQfNnXYPR62oqml8bWXO0SR2OJdJ7XXn0CS5Uuv/mH8cYQQb3K NPwEZ6DpHBF+8ayr4rYG70PO6mE1IjDtFSwLSrAIv/xtMP7X7eV170pfUR15RV0AIbP9 FJKLAOCrR5DUFh1pfOvcSCXKoIoUUVJRBGn/mBiiXO1e85lHQcdin2w9Gq93kkdRne2u xeBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ywH0nem5xK3VdxkEfKzK+pLHZ+0rEC92xbwaiP1TgDc=; b=tDL7oubzjy4Hx+ySV2ruhYu+67RWpLpav1ylXbbjwRCNdnwGKkmLVfDoiUjZSzq3JH 0V4ytTQe6rpSVTZpWAl/fRTlZM3C+klBTFfCC1DY+vGjeU/MZ72zafgnItPuR6FpUOl1 WDe4Z5EDyRp8y6MZW7YrL2ooSGjoRBnFpxQ5OXXNjxE1h+H/KsML/qa8zJIZcVkySHox o1tbkNXuZUx7eb8yfWTY78q7xtIcNfBee3BP0DaeRFuLpdvkZH6BSOlOTL0GRhiwg4bh zuySVqzNvLUR5ugA3IYw0/U/HFTz2d6eAIUBMdNH+6TmJQZMtObnLLYx5RF2qEq0pFkj jQ/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EDDN8wvU; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m20si10193201pgk.323.2019.01.21.06.03.35; Mon, 21 Jan 2019 06:04:00 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EDDN8wvU; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731773AbfAUOAC (ORCPT + 99 others); Mon, 21 Jan 2019 09:00:02 -0500 Received: from mail.kernel.org ([198.145.29.99]:46266 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732293AbfAUOAA (ORCPT ); Mon, 21 Jan 2019 09:00:00 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B8E6B2087F; Mon, 21 Jan 2019 13:59:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548079199; bh=8X89epfTPxoZttnMGavahmKESlm/LzEPaYStJRevPvc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EDDN8wvUXy4sgvomb4LE+cx5h4kNu5Hru7DZ5+163ORjuR0XQUG0z4ys8XJ0GYnJM tcmH7E7731BK6ydfFe72TkUzFl4jz6T0ESuD2k7kd22HjwXa7fG4nPu7M93m4syXjO wwwTWrbTlm4pihu7sK+uNzG9x9rXIfZFqVGZGie4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Shawn Bohrer , Florian Westphal , Pablo Neira Ayuso Subject: [PATCH 4.19 08/99] netfilter: nf_conncount: split gc in two phases Date: Mon, 21 Jan 2019 14:48:00 +0100 Message-Id: <20190121134914.234510860@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190121134913.924726465@linuxfoundation.org> References: <20190121134913.924726465@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Florian Westphal commit f7fcc98dfc2d136722007fec0debbed761679b94 upstream. The lockless workqueue garbage collector can race with packet path garbage collector to delete list nodes, as it calls tree_nodes_free() with the addresses of nodes that might have been free'd already from another cpu. To fix this, split gc into two phases. One phase to perform gc on the connections: From a locking perspective, this is the same as count_tree(): we hold rcu lock, but we do not change the tree, we only change the nodes' contents. The second phase acquires the tree lock and reaps empty nodes. This avoids a race condition of the garbage collection vs. packet path: If a node has been free'd already, the second phase won't find it anymore. This second phase is, from locking perspective, same as insert_tree(). The former only modifies nodes (list content, count), latter modifies the tree itself (rb_erase or rb_insert). Fixes: 5c789e131cbb9 ("netfilter: nf_conncount: Add list lock and gc worker, and RCU for init tree search") Reviewed-by: Shawn Bohrer Signed-off-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Greg Kroah-Hartman --- net/netfilter/nf_conncount.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) --- a/net/netfilter/nf_conncount.c +++ b/net/netfilter/nf_conncount.c @@ -500,16 +500,32 @@ static void tree_gc_worker(struct work_s for (node = rb_first(root); node != NULL; node = rb_next(node)) { rbconn = rb_entry(node, struct nf_conncount_rb, node); if (nf_conncount_gc_list(data->net, &rbconn->list)) - gc_nodes[gc_count++] = rbconn; + gc_count++; } rcu_read_unlock(); spin_lock_bh(&nf_conncount_locks[tree]); + if (gc_count < ARRAY_SIZE(gc_nodes)) + goto next; /* do not bother */ - if (gc_count) { - tree_nodes_free(root, gc_nodes, gc_count); + gc_count = 0; + node = rb_first(root); + while (node != NULL) { + rbconn = rb_entry(node, struct nf_conncount_rb, node); + node = rb_next(node); + + if (rbconn->list.count > 0) + continue; + + gc_nodes[gc_count++] = rbconn; + if (gc_count >= ARRAY_SIZE(gc_nodes)) { + tree_nodes_free(root, gc_nodes, gc_count); + gc_count = 0; + } } + tree_nodes_free(root, gc_nodes, gc_count); +next: clear_bit(tree, data->pending_trees); next_tree = (tree + 1) % CONNCOUNT_SLOTS;