Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3782913ybi; Mon, 29 Jul 2019 12:32:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqwRbV3Cw4fOdkY5JfZZrHK6Tu+xOOD5GYiUscrWXAzqJsCE1uoCG6t8C/hCVPX6RSGtg9Bf X-Received: by 2002:a17:90a:974b:: with SMTP id i11mr112275205pjw.21.1564428731849; Mon, 29 Jul 2019 12:32:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564428731; cv=none; d=google.com; s=arc-20160816; b=vQFjih+MiZI5wsAeCa2Yp40a9C9ESiZwu0Zez33sRWZAc60zPXGxk1O/nSV3vEBCfI VjMep0XYShNJpcuyJdZU2vPlWIy8XdJEz1GqHN3/Koc/UK5zWVzIU8BPQRC64Frad5u/ mt3HpJZDJDun2WAEcWy0JY9Wj6umAPkGOiWEniHBfeSlskILnF58NIJ6IWbBPvyQhoM+ +7OWWDXhFLYChXzMwh1na3cD+VSgjtQRp7vCgZBr4lUmrY2EoyuFskTP3U8zNg4kCQOZ jq4r+W2lnFrf1xm8B/N6TRqrUDutfJfP05d8FtX+iqnGG1GAtUg2XsXGcNBu4aJwm/eJ luvQ== 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=jX1DgvpvjpLyUTcfl0ObRNFaJOyRUvOMNU0IoW7YQ/I=; b=gP76qh7UCFJIg68wKGTBH6eT2IIKsLe7ysjQ42H9UQZF/AxH6tM/Mrd3dh2gb5gDXP K7bjZ3aoltRmpmD8ZykTgAPbEQsI1DKCruwCCeKtKdVz8/MZz+IY+xJR9S+cfDK1/79M 4geX4Ihh+R9zqjXhayHKt65LfVXtAj63heDACcsdorMFlsaYl1sVsgldCKv6NxFxriXy wAX1Kfz5iIuzyi6BFNOxFLBWd6lOwkJV4/AbsReKoZ+SEy5gjL9u4GSxLJhgllB4NfYs feqyNrOD3xmMXXTS4wt7ecUJlAXC7k97Kfh5YFf4C2L+4Ht20lpEMnOrLHfoozYjj5XI Osng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nElel3pS; 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 l24si30772897pff.66.2019.07.29.12.31.56; Mon, 29 Jul 2019 12:32:11 -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=nElel3pS; 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 S1728876AbfG2TZ3 (ORCPT + 99 others); Mon, 29 Jul 2019 15:25:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:37748 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726704AbfG2TZ1 (ORCPT ); Mon, 29 Jul 2019 15:25:27 -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 2EBCD2070B; Mon, 29 Jul 2019 19:25:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564428326; bh=9KPMqcFVl76BOyI1j5wYg17cZF9m0IwBYUpEG27SEQ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nElel3pS+rgrgGZUnXCJNXrm/qSSxodaiBwlBTKZbJeMZLplAW5BCKYXD8S856cmR 0nYsevOS9NI/a9dXbTV+06e4Cf/LqUeUjAYsfCjR0FLGEaqgyEhm4f1UYRTZPlzZn7 j4WWThPILbEPjAMSigXhumSp8l6qCbnGBmsLQyO8= 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 4.14 040/293] ipset: Fix memory accounting for hash types on resize Date: Mon, 29 Jul 2019 21:18:51 +0200 Message-Id: <20190729190825.614326546@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729190820.321094988@linuxfoundation.org> References: <20190729190820.321094988@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 dfd268166e42..42d9cd22447e 100644 --- a/net/netfilter/ipset/ip_set_hash_gen.h +++ b/net/netfilter/ipset/ip_set_hash_gen.h @@ -624,7 +624,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