Received: by 10.223.185.116 with SMTP id b49csp823336wrg; Sat, 10 Feb 2018 21:13:59 -0800 (PST) X-Google-Smtp-Source: AH8x224IqxkBedHN9GlhbMZhzFYp9BKSA0afQE4XMubq0sA0mMUiAcRe3ErTTs8X6r8btsQmW/JC X-Received: by 2002:a17:902:8e8a:: with SMTP id bg10-v6mr7409716plb.162.1518326039678; Sat, 10 Feb 2018 21:13:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518326039; cv=none; d=google.com; s=arc-20160816; b=hYzBFNUFT/tTL7ZSGAt0hNuuGSpLm6qAcPveVroe5+EZ3uKKQ3ZaYSkdDESA4T72wF IMMtuKCbzRzzPKNaTQAQ13CDQsgudnaf8PEWWDEjIsznq2mjcQSoK91nTvBqnFpNuiVg bcgca6Qx4eYnqCPRuTUDK37bWhYyRAg0ybB7+q+VuFKCQ71VrnyZbuervKsmh9zDveYQ wK5khK4VywAv6pNQ+ukRfijSB091HhnuWXpH0ruCBe+2d1EscTrIx34PdGFG6he4nai7 mJKXDYQHkzyf0/2DoF5dgC8O+WOJZG6J4uHMq12RoPffnw/q9ndWtZ/UnSrMLNp31Hgn UGCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=IOYUKknV4qoEEdbitFHNwXufHch8IEjWKcEnQlkOG+0=; b=RfVcf6jOcNePhCx10BB41gTjmDcenU+eFevCcjQyWt050uwU1ubJFo8Uo0+OkzfZOG DRuaRpHDReP6kDSEgvmoiff8boiF3rWqtf36ZKsw54djkaLxBxbvUQiMZuBWgl+OmRiR FNukGG5aqauIcteomu6YsjL96gFmZ7TzZzEPfGrmMFWHn6WOcCnkYcH2sURhZItlXBrV +23XTyMaXpqEsC2XOhq0DuuePH3TYRJxWg+EPX3gFhVMvPDFuDU05xwPC/TDrSi+1mf6 P13bOpP0kQCdpIEVVIgSG0EURcRJMS/qhxkVcaS8TmTd1+xSobdiW5o77ArhC4v4AfS9 dYNQ== ARC-Authentication-Results: i=1; mx.google.com; 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 t9si3557675pge.540.2018.02.10.21.13.46; Sat, 10 Feb 2018 21:13:59 -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; 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 S1754004AbeBKFMb (ORCPT + 99 others); Sun, 11 Feb 2018 00:12:31 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:41421 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752638AbeBKEdl (ORCPT ); Sat, 10 Feb 2018 23:33:41 -0500 Received: from [2a02:8011:400e:2:6f00:88c8:c921:d332] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ekjKe-0002hS-7U; Sun, 11 Feb 2018 04:33:40 +0000 Received: from ben by deadeye with local (Exim 4.90) (envelope-from ) id 1ekjKY-0004V3-6h; Sun, 11 Feb 2018 04:33:34 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Mike Snitzer" , "Eric Biggers" Date: Sun, 11 Feb 2018 04:20:06 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.2 45/79] dm bufio: fix integer overflow when limiting maximum cache size In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:6f00:88c8:c921:d332 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.2.99-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Eric Biggers commit 74d4108d9e681dbbe4a2940ed8fdff1f6868184c upstream. The default max_cache_size_bytes for dm-bufio is meant to be the lesser of 25% of the size of the vmalloc area and 2% of the size of lowmem. However, on 32-bit systems the intermediate result in the expression (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100 overflows, causing the wrong result to be computed. For example, on a 32-bit system where the vmalloc area is 520093696 bytes, the result is 1174405 rather than the expected 130023424, which makes the maximum cache size much too small (far less than 2% of lowmem). This causes severe performance problems for dm-verity users on affected systems. Fix this by using mult_frac() to correctly multiply by a percentage. Do this for all places in dm-bufio that multiply by a percentage. Also replace (VMALLOC_END - VMALLOC_START) with VMALLOC_TOTAL, which contrary to the comment is now defined in include/linux/vmalloc.h. Depends-on: 9993bc635 ("sched/x86: Fix overflow in cyc2ns_offset") Fixes: 95d402f057f2 ("dm: add bufio") Signed-off-by: Eric Biggers Signed-off-by: Mike Snitzer [bwh: Backported to 3.2: keep open-coded VMALLOC_TOTAL] Signed-off-by: Ben Hutchings --- drivers/md/dm-bufio.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) --- a/drivers/md/dm-bufio.c +++ b/drivers/md/dm-bufio.c @@ -839,7 +839,8 @@ static void __get_memory_limit(struct dm buffers = DM_BUFIO_MIN_BUFFERS; *limit_buffers = buffers; - *threshold_buffers = buffers * DM_BUFIO_WRITEBACK_PERCENT / 100; + *threshold_buffers = mult_frac(buffers, + DM_BUFIO_WRITEBACK_PERCENT, 100); } /* @@ -1620,19 +1621,15 @@ static int __init dm_bufio_init(void) memset(&dm_bufio_caches, 0, sizeof dm_bufio_caches); memset(&dm_bufio_cache_names, 0, sizeof dm_bufio_cache_names); - mem = (__u64)((totalram_pages - totalhigh_pages) * - DM_BUFIO_MEMORY_PERCENT / 100) << PAGE_SHIFT; + mem = (__u64)mult_frac(totalram_pages - totalhigh_pages, + DM_BUFIO_MEMORY_PERCENT, 100) << PAGE_SHIFT; if (mem > ULONG_MAX) mem = ULONG_MAX; #ifdef CONFIG_MMU - /* - * Get the size of vmalloc space the same way as VMALLOC_TOTAL - * in fs/proc/internal.h - */ - if (mem > (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100) - mem = (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100; + if (mem > mult_frac(VMALLOC_END - VMALLOC_START, DM_BUFIO_VMALLOC_PERCENT, 100)) + mem = mult_frac(VMALLOC_END - VMALLOC_START, DM_BUFIO_VMALLOC_PERCENT, 100); #endif dm_bufio_default_cache_size = mem;