Skip to content

Commit af73483

Browse files
Matthew Wilcox (Oracle)torvalds
authored andcommitted
ida: Fix crash in ida_free when the bitmap is empty
The IDA usually detects double-frees, but that detection failed to consider the case when there are no nearby IDs allocated and so we have a NULL bitmap rather than simply having a clear bit. Add some tests to the test-suite to be sure we don't inadvertently reintroduce this problem. Unfortunately they're quite noisy so include a message to disregard the warnings. Reported-by: Zhenghan Wang <[email protected]> Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent a9e01ac commit af73483

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/idr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ void ida_free(struct ida *ida, unsigned int id)
508508
goto delete;
509509
xas_store(&xas, xa_mk_value(v));
510510
} else {
511-
if (!test_bit(bit, bitmap->bitmap))
511+
if (!bitmap || !test_bit(bit, bitmap->bitmap))
512512
goto err;
513513
__clear_bit(bit, bitmap->bitmap);
514514
xas_set_mark(&xas, XA_FREE_MARK);

lib/test_ida.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,45 @@ static void ida_check_conv(struct ida *ida)
150150
IDA_BUG_ON(ida, !ida_is_empty(ida));
151151
}
152152

153+
/*
154+
* Check various situations where we attempt to free an ID we don't own.
155+
*/
156+
static void ida_check_bad_free(struct ida *ida)
157+
{
158+
unsigned long i;
159+
160+
printk("vvv Ignore \"not allocated\" warnings\n");
161+
/* IDA is empty; all of these will fail */
162+
ida_free(ida, 0);
163+
for (i = 0; i < 31; i++)
164+
ida_free(ida, 1 << i);
165+
166+
/* IDA contains a single value entry */
167+
IDA_BUG_ON(ida, ida_alloc_min(ida, 3, GFP_KERNEL) != 3);
168+
ida_free(ida, 0);
169+
for (i = 0; i < 31; i++)
170+
ida_free(ida, 1 << i);
171+
172+
/* IDA contains a single bitmap */
173+
IDA_BUG_ON(ida, ida_alloc_min(ida, 1023, GFP_KERNEL) != 1023);
174+
ida_free(ida, 0);
175+
for (i = 0; i < 31; i++)
176+
ida_free(ida, 1 << i);
177+
178+
/* IDA contains a tree */
179+
IDA_BUG_ON(ida, ida_alloc_min(ida, (1 << 20) - 1, GFP_KERNEL) != (1 << 20) - 1);
180+
ida_free(ida, 0);
181+
for (i = 0; i < 31; i++)
182+
ida_free(ida, 1 << i);
183+
printk("^^^ \"not allocated\" warnings over\n");
184+
185+
ida_free(ida, 3);
186+
ida_free(ida, 1023);
187+
ida_free(ida, (1 << 20) - 1);
188+
189+
IDA_BUG_ON(ida, !ida_is_empty(ida));
190+
}
191+
153192
static DEFINE_IDA(ida);
154193

155194
static int ida_checks(void)
@@ -162,6 +201,7 @@ static int ida_checks(void)
162201
ida_check_leaf(&ida, 1024 * 64);
163202
ida_check_max(&ida);
164203
ida_check_conv(&ida);
204+
ida_check_bad_free(&ida);
165205

166206
printk("IDA: %u of %u tests passed\n", tests_passed, tests_run);
167207
return (tests_run != tests_passed) ? 0 : -EINVAL;

0 commit comments

Comments
 (0)