Skip to content

Commit 8532d3e

Browse files
Huang Shijiegregkh
authored andcommitted
lib/genalloc: fix the overflow when size is too big
[ Upstream commit 3684566 ] 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 <sjhuang@iluvatar.ai> Reported-by: Shi Jiasheng <jiasheng.shi@iluvatar.ai> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 2179bae commit 8532d3e

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

‎lib/genalloc.c‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ static int clear_bits_ll(unsigned long *addr, unsigned long mask_to_clear)
8181
* users set the same bit, one user will return remain bits, otherwise
8282
* return 0.
8383
*/
84-
static int bitmap_set_ll(unsigned long *map, int start, int nr)
84+
static int bitmap_set_ll(unsigned long *map, unsigned long start, unsigned long nr)
8585
{
8686
unsigned long *p = map + BIT_WORD(start);
87-
const int size = start + nr;
87+
const unsigned long size = start + nr;
8888
int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
8989
unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
9090

91-
while (nr - bits_to_set >= 0) {
91+
while (nr >= bits_to_set) {
9292
if (set_bits_ll(p, mask_to_set))
9393
return nr;
9494
nr -= bits_to_set;
@@ -116,14 +116,15 @@ static int bitmap_set_ll(unsigned long *map, int start, int nr)
116116
* users clear the same bit, one user will return remain bits,
117117
* otherwise return 0.
118118
*/
119-
static int bitmap_clear_ll(unsigned long *map, int start, int nr)
119+
static unsigned long
120+
bitmap_clear_ll(unsigned long *map, unsigned long start, unsigned long nr)
120121
{
121122
unsigned long *p = map + BIT_WORD(start);
122-
const int size = start + nr;
123+
const unsigned long size = start + nr;
123124
int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
124125
unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
125126

126-
while (nr - bits_to_clear >= 0) {
127+
while (nr >= bits_to_clear) {
127128
if (clear_bits_ll(p, mask_to_clear))
128129
return nr;
129130
nr -= bits_to_clear;
@@ -183,8 +184,8 @@ int gen_pool_add_owner(struct gen_pool *pool, unsigned long virt, phys_addr_t ph
183184
size_t size, int nid, void *owner)
184185
{
185186
struct gen_pool_chunk *chunk;
186-
int nbits = size >> pool->min_alloc_order;
187-
int nbytes = sizeof(struct gen_pool_chunk) +
187+
unsigned long nbits = size >> pool->min_alloc_order;
188+
unsigned long nbytes = sizeof(struct gen_pool_chunk) +
188189
BITS_TO_LONGS(nbits) * sizeof(long);
189190

190191
chunk = vzalloc_node(nbytes, nid);
@@ -242,7 +243,7 @@ void gen_pool_destroy(struct gen_pool *pool)
242243
struct list_head *_chunk, *_next_chunk;
243244
struct gen_pool_chunk *chunk;
244245
int order = pool->min_alloc_order;
245-
int bit, end_bit;
246+
unsigned long bit, end_bit;
246247

247248
list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
248249
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,
278279
struct gen_pool_chunk *chunk;
279280
unsigned long addr = 0;
280281
int order = pool->min_alloc_order;
281-
int nbits, start_bit, end_bit, remain;
282+
unsigned long nbits, start_bit, end_bit, remain;
282283

283284
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
284285
BUG_ON(in_nmi());
@@ -487,7 +488,7 @@ void gen_pool_free_owner(struct gen_pool *pool, unsigned long addr, size_t size,
487488
{
488489
struct gen_pool_chunk *chunk;
489490
int order = pool->min_alloc_order;
490-
int start_bit, nbits, remain;
491+
unsigned long start_bit, nbits, remain;
491492

492493
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
493494
BUG_ON(in_nmi());
@@ -755,7 +756,7 @@ unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size,
755756
index = bitmap_find_next_zero_area(map, size, start, nr, 0);
756757

757758
while (index < size) {
758-
int next_bit = find_next_bit(map, size, index + nr);
759+
unsigned long next_bit = find_next_bit(map, size, index + nr);
759760
if ((next_bit - index) < len) {
760761
len = next_bit - index;
761762
start_bit = index;

0 commit comments

Comments
 (0)