Skip to content

Commit 74d4108

Browse files
ebiggerssnitm
authored andcommitted
dm bufio: fix integer overflow when limiting maximum cache size
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: 9993bc6 ("sched/x86: Fix overflow in cyc2ns_offset") Fixes: 95d402f ("dm: add bufio") Cc: <stable@vger.kernel.org> # v3.2+ Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Mike Snitzer <snitzer@redhat.com>
1 parent 5d47c89 commit 74d4108

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

‎drivers/md/dm-bufio.c‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,8 @@ static void __get_memory_limit(struct dm_bufio_client *c,
974974
buffers = c->minimum_buffers;
975975

976976
*limit_buffers = buffers;
977-
*threshold_buffers = buffers * DM_BUFIO_WRITEBACK_PERCENT / 100;
977+
*threshold_buffers = mult_frac(buffers,
978+
DM_BUFIO_WRITEBACK_PERCENT, 100);
978979
}
979980

980981
/*
@@ -1910,19 +1911,15 @@ static int __init dm_bufio_init(void)
19101911
memset(&dm_bufio_caches, 0, sizeof dm_bufio_caches);
19111912
memset(&dm_bufio_cache_names, 0, sizeof dm_bufio_cache_names);
19121913

1913-
mem = (__u64)((totalram_pages - totalhigh_pages) *
1914-
DM_BUFIO_MEMORY_PERCENT / 100) << PAGE_SHIFT;
1914+
mem = (__u64)mult_frac(totalram_pages - totalhigh_pages,
1915+
DM_BUFIO_MEMORY_PERCENT, 100) << PAGE_SHIFT;
19151916

19161917
if (mem > ULONG_MAX)
19171918
mem = ULONG_MAX;
19181919

19191920
#ifdef CONFIG_MMU
1920-
/*
1921-
* Get the size of vmalloc space the same way as VMALLOC_TOTAL
1922-
* in fs/proc/internal.h
1923-
*/
1924-
if (mem > (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100)
1925-
mem = (VMALLOC_END - VMALLOC_START) * DM_BUFIO_VMALLOC_PERCENT / 100;
1921+
if (mem > mult_frac(VMALLOC_TOTAL, DM_BUFIO_VMALLOC_PERCENT, 100))
1922+
mem = mult_frac(VMALLOC_TOTAL, DM_BUFIO_VMALLOC_PERCENT, 100);
19261923
#endif
19271924

19281925
dm_bufio_default_cache_size = mem;

0 commit comments

Comments
 (0)