Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp2260518pxb; Mon, 11 Jan 2021 05:26:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJxaydZAgMTe9I65r/X6KGuKuPRhe5OagAQKrdSA9m17oOFvXVDXk9ID4F0ZVVZOTx0ZgHnz X-Received: by 2002:a17:907:2d8b:: with SMTP id gt11mr4442646ejc.32.1610371567171; Mon, 11 Jan 2021 05:26:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610371567; cv=none; d=google.com; s=arc-20160816; b=REHpN+kdSdxipx6hBRlntMD8MgP00eCN7uPK/GHQpDVoMN6caAOGgZjpU6St5UPjCe Ex4yBzurqm/kbwIUpH0JUmZse3NKp1sYPsXplxZjEknIbNU9yFPmZh2akbIuaXZ8XEI0 OZwxNvLmUtM1obc2vePKGJQhv+ntRDZj5lVK19BCESyr1y/QKA7iUl2Iep5489nGaAjZ 1Htb5Gu0e50gndG1QGYJ+4ovt+ZC4NpnJnrebPYO8yDESTnJUp2zKT1idswoe3UbjH3D hHgZPEIWtk8NZqGeVqWQAmNj/9rZKMrV1LAxkAEEViDTsNXwZo6fWWPfIAAWB7yDAzqn SbVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=1LDdhHgXs5yKcF4SMxXk2vEbzPH9faDlkTXNHKqNT1E=; b=FFlB3tJlcor6px1sKxVEcIouDGvJVMP258MY7/oSNrHxnOjMqpELmu0+PaWlqwKDM6 NF4yg2O4QkQzKBPohCPD8INtPdO7vMnkXfcj7VTmXL8wW3Hk7PAARjbHOpLqATR6DBwg ItNfthWqyHvktR9Hnht0TBfxPAXpXf7x63Lh5YOJBXawaUaRsKZiebmxYjXHy9aEcpte pvke6D8W+Ex6cl9j45DqOrjOrlEwtYhkuWcaRbhvDCqmhbqC6LI8T+T2uXhEbPvXmPl0 3SodGLXEyu8ZG7W+lZikSYOxeqZLUeHQjRotsVHGAgpVPf8rMZQPA2k3+KAbSByw6jlr rgeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UbwGO9pW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lr5si6476152ejb.59.2021.01.11.05.25.43; Mon, 11 Jan 2021 05:26:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UbwGO9pW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730826AbhAKNMC (ORCPT + 99 others); Mon, 11 Jan 2021 08:12:02 -0500 Received: from mail.kernel.org ([198.145.29.99]:58310 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731345AbhAKNLM (ORCPT ); Mon, 11 Jan 2021 08:11:12 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0C70021534; Mon, 11 Jan 2021 13:10:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1610370631; bh=L47XDru5DO/x57PYjBmpn9hhBR5f2Vn1dNZcEwIfZ38=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UbwGO9pWvaNiHh47G6FdKSiqi8IPtvZCpuCCyD0y4GtfZ4pnq23beUAyGr6kggg8x ddwZQNFoXkWUPb62LQ4YK50QQ2XDnozwrSOkLKDuv4qevI/6CcMnjtTZLAcSM4Tzhy aZ/IYR/wtKspqNdc1yrmBQ68ihYvkryN593AO9M8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Huang Shijie , Shi Jiasheng , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.4 06/92] lib/genalloc: fix the overflow when size is too big Date: Mon, 11 Jan 2021 14:01:10 +0100 Message-Id: <20210111130039.468790192@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210111130039.165470698@linuxfoundation.org> References: <20210111130039.165470698@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Huang Shijie [ Upstream commit 36845663843fc59c5d794e3dc0641472e3e572da ] Some graphic card has very big memory on chip, such as 32G bytes. In the following case, it will cause overflow: pool = gen_pool_create(PAGE_SHIFT, NUMA_NO_NODE); ret = gen_pool_add(pool, 0x1000000, SZ_32G, NUMA_NO_NODE); va = gen_pool_alloc(pool, SZ_4G); The overflow occurs in gen_pool_alloc_algo_owner(): .... size = nbits << order; .... The @nbits is "int" type, so it will overflow. Then the gen_pool_avail() will return the wrong value. This patch converts some "int" to "unsigned long", and changes the compare code in while. Link: https://lkml.kernel.org/r/20201229060657.3389-1-sjhuang@iluvatar.ai Signed-off-by: Huang Shijie Reported-by: Shi Jiasheng Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- lib/genalloc.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/genalloc.c b/lib/genalloc.c index 9fc31292cfa1d..80d10d02cf388 100644 --- a/lib/genalloc.c +++ b/lib/genalloc.c @@ -81,14 +81,14 @@ static int clear_bits_ll(unsigned long *addr, unsigned long mask_to_clear) * users set the same bit, one user will return remain bits, otherwise * return 0. */ -static int bitmap_set_ll(unsigned long *map, int start, int nr) +static int bitmap_set_ll(unsigned long *map, unsigned long start, unsigned long nr) { unsigned long *p = map + BIT_WORD(start); - const int size = start + nr; + const unsigned long size = start + nr; int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); - while (nr - bits_to_set >= 0) { + while (nr >= bits_to_set) { if (set_bits_ll(p, mask_to_set)) return nr; nr -= bits_to_set; @@ -116,14 +116,15 @@ static int bitmap_set_ll(unsigned long *map, int start, int nr) * users clear the same bit, one user will return remain bits, * otherwise return 0. */ -static int bitmap_clear_ll(unsigned long *map, int start, int nr) +static unsigned long +bitmap_clear_ll(unsigned long *map, unsigned long start, unsigned long nr) { unsigned long *p = map + BIT_WORD(start); - const int size = start + nr; + const unsigned long size = start + nr; int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start); - while (nr - bits_to_clear >= 0) { + while (nr >= bits_to_clear) { if (clear_bits_ll(p, mask_to_clear)) return nr; nr -= bits_to_clear; @@ -183,8 +184,8 @@ int gen_pool_add_owner(struct gen_pool *pool, unsigned long virt, phys_addr_t ph size_t size, int nid, void *owner) { struct gen_pool_chunk *chunk; - int nbits = size >> pool->min_alloc_order; - int nbytes = sizeof(struct gen_pool_chunk) + + unsigned long nbits = size >> pool->min_alloc_order; + unsigned long nbytes = sizeof(struct gen_pool_chunk) + BITS_TO_LONGS(nbits) * sizeof(long); chunk = vzalloc_node(nbytes, nid); @@ -242,7 +243,7 @@ void gen_pool_destroy(struct gen_pool *pool) struct list_head *_chunk, *_next_chunk; struct gen_pool_chunk *chunk; int order = pool->min_alloc_order; - int bit, end_bit; + unsigned long bit, end_bit; list_for_each_safe(_chunk, _next_chunk, &pool->chunks) { chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk); @@ -278,7 +279,7 @@ unsigned long gen_pool_alloc_algo_owner(struct gen_pool *pool, size_t size, struct gen_pool_chunk *chunk; unsigned long addr = 0; int order = pool->min_alloc_order; - int nbits, start_bit, end_bit, remain; + unsigned long nbits, start_bit, end_bit, remain; #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG BUG_ON(in_nmi()); @@ -487,7 +488,7 @@ void gen_pool_free_owner(struct gen_pool *pool, unsigned long addr, size_t size, { struct gen_pool_chunk *chunk; int order = pool->min_alloc_order; - int start_bit, nbits, remain; + unsigned long start_bit, nbits, remain; #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG BUG_ON(in_nmi()); @@ -754,7 +755,7 @@ unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size, index = bitmap_find_next_zero_area(map, size, start, nr, 0); while (index < size) { - int next_bit = find_next_bit(map, size, index + nr); + unsigned long next_bit = find_next_bit(map, size, index + nr); if ((next_bit - index) < len) { len = next_bit - index; start_bit = index; -- 2.27.0