Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9928781ybi; Wed, 24 Jul 2019 12:30:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqwha0PPeThdYMW9IMY2VW9/urXqV794HG4ipBkdPrITuS49YTXKku+uxeIARP/FkaSZy/lv X-Received: by 2002:a17:902:8bc1:: with SMTP id r1mr38468063plo.42.1563996617758; Wed, 24 Jul 2019 12:30:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563996617; cv=none; d=google.com; s=arc-20160816; b=Uhce0u2dV5Np56T/SiLTSeeU+esaflh99Scz8Z/n3PYfJzcboouCwbG2RSrkkxdQNC J1stBQu67r7cFI8xNQ5QEiiN+2uFSu2pR0KdIS7bzmOsIiuP0IJ+JmukbrwBkIaD4imo gKgW6D8pW8rCQVnoajofEJJgCLqPhHUsWfV60xbD/1OIzc55+k5PoZZe1B+Vtc8u/VQE /lkTalJ8Jt+gifBCueCoMXvH8bNKmilzyoGWNNg3qBD/6SShfH9ZJZtMWBulAPUcGtqN lnq0EsFCY8ooN4gI7WcxrYdXMHIRlx02MHK1WhzusFUd/FVW+RYl2Jz6eGdhFM6QfxTk McaA== 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=zD3k/yLEDsgUEq6i1iCK4B4eMGa7MUJrfJ+giS0ylN8=; b=D0uXNfS64CkqxODpPid2ZcTDHKOE+pFmVrfGlv2rcrNOXPJUStuU+q7rch+6hOSGaN GTx7UaQ5cSPFgKBOBs2E3E/KXeM1OpG2jlY+T7aKUM+Ut0d+0GIi41zH1jk432L7Pysx j0TFeyb4L5G+pfWF/eJFqj3SbkwgZW57IKQS0KXjJOzTjeThIbijCq2U4XbaLOjWCQIs cMtxnCNJ1sxmd7xHn/9ldCb8A6eA1cU0b/1ry78GTSi32XW2oFRUdV5zQqqBNV/AGrLu ZiLFjJ/qQJAbjnFHr2oF2b/G5vFOni45B/WoiKx6P4IM8SAdaPt42v+6CdqaxYutFGbD U02g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HszOM5gZ; 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 a14si14376015pjo.40.2019.07.24.12.30.03; Wed, 24 Jul 2019 12:30:17 -0700 (PDT) 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=HszOM5gZ; 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 S1728637AbfGXT1s (ORCPT + 99 others); Wed, 24 Jul 2019 15:27:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:45614 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388141AbfGXT1o (ORCPT ); Wed, 24 Jul 2019 15:27:44 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.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 A4C32229F3; Wed, 24 Jul 2019 19:27:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563996463; bh=Cn7BozbWGXy8u2TffAHqQHE+mfuQkgHdCQiFY9C5FCw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HszOM5gZl2RXMkBSm+CSiv0UiQBJKZ5IcJsIafSmzQXve+2g7z9C3jjBTGRXztxef UjP87XNqBz6qN1KzuIKDwnVAYiNibd3Dqrf6zd4jGUF2CR0Nf6B6zwH9+kZOkBzkG3 iat3IjxwK1bJnPnxz66rCinBXXhdj4VbWBgvylCY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, NOYB , Stefano Brivio , Jozsef Kadlecsik , Sasha Levin Subject: [PATCH 5.2 078/413] ipset: Fix memory accounting for hash types on resize Date: Wed, 24 Jul 2019 21:16:09 +0200 Message-Id: <20190724191740.678737351@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190724191735.096702571@linuxfoundation.org> References: <20190724191735.096702571@linuxfoundation.org> User-Agent: quilt/0.66 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 [ Upstream commit 11921796f4799ca9c61c4b22cc54d84aa69f8a35 ] If a fresh array block is allocated during resize, the current in-memory set size should be increased by the size of the block, not replaced by it. Before the fix, adding entries to a hash set type, leading to a table resize, caused an inconsistent memory size to be reported. This becomes more obvious when swapping sets with similar sizes: # cat hash_ip_size.sh #!/bin/sh FAIL_RETRIES=10 tries=0 while [ ${tries} -lt ${FAIL_RETRIES} ]; do ipset create t1 hash:ip for i in `seq 1 4345`; do ipset add t1 1.2.$((i / 255)).$((i % 255)) done t1_init="$(ipset list t1|sed -n 's/Size in memory: \(.*\)/\1/p')" ipset create t2 hash:ip for i in `seq 1 4360`; do ipset add t2 1.2.$((i / 255)).$((i % 255)) done t2_init="$(ipset list t2|sed -n 's/Size in memory: \(.*\)/\1/p')" ipset swap t1 t2 t1_swap="$(ipset list t1|sed -n 's/Size in memory: \(.*\)/\1/p')" t2_swap="$(ipset list t2|sed -n 's/Size in memory: \(.*\)/\1/p')" ipset destroy t1 ipset destroy t2 tries=$((tries + 1)) if [ ${t1_init} -lt 10000 ] || [ ${t2_init} -lt 10000 ]; then echo "FAIL after ${tries} tries:" echo "T1 size ${t1_init}, after swap ${t1_swap}" echo "T2 size ${t2_init}, after swap ${t2_swap}" exit 1 fi done echo "PASS" # echo -n 'func hash_ip4_resize +p' > /sys/kernel/debug/dynamic_debug/control # ./hash_ip_size.sh [ 2035.018673] attempt to resize set t1 from 10 to 11, t 00000000fe6551fa [ 2035.078583] set t1 resized from 10 (00000000fe6551fa) to 11 (00000000172a0163) [ 2035.080353] Table destroy by resize 00000000fe6551fa FAIL after 4 tries: T1 size 9064, after swap 71128 T2 size 71128, after swap 9064 Reported-by: NOYB Fixes: 9e41f26a505c ("netfilter: ipset: Count non-static extension memory for userspace") Signed-off-by: Stefano Brivio Signed-off-by: Jozsef Kadlecsik Signed-off-by: Sasha Levin --- net/netfilter/ipset/ip_set_hash_gen.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h index 10f619625abd..175f8fedcfaf 100644 --- a/net/netfilter/ipset/ip_set_hash_gen.h +++ b/net/netfilter/ipset/ip_set_hash_gen.h @@ -622,7 +622,7 @@ mtype_resize(struct ip_set *set, bool retried) goto cleanup; } m->size = AHASH_INIT_SIZE; - extsize = ext_size(AHASH_INIT_SIZE, dsize); + extsize += ext_size(AHASH_INIT_SIZE, dsize); RCU_INIT_POINTER(hbucket(t, key), m); } else if (m->pos >= m->size) { struct hbucket *ht; -- 2.20.1