Commit cb2d3883 authored by Denis Kirjanov's avatar Denis Kirjanov Committed by Michael Ellerman

powerpc/msi: Free the bitmap if it was slab allocated

During the MSI bitmap test on boot kmemleak spews the following trace:

unreferenced object 0xc00000016e86c900 (size 64):
    comm "swapper/0", pid 1, jiffies 4294893173 (age 518.024s)
    hex dump (first 32 bytes):
	00 00 01 ff 7f ff 7f 37 00 00 00 00 00 00 00 00
	.......7........
	ff ff ff ff ff ff ff ff 01 ff ff ff ff
	ff ff ff
	................
	backtrace:
	[<c00000000003eebc>] .zalloc_maybe_bootmem+0x3c/0x380
	[<c000000000042d6c>] .msi_bitmap_alloc+0x3c/0xb0
	[<c000000000a9aff8>] .msi_bitmap_selftest+0x30/0x2b4
	[<c0000000000090f4>] .do_one_initcall+0xd4/0x270
	[<c000000000a8e250>] .kernel_init_freeable+0x1a0/0x280
	[<c000000000009b5c>] .kernel_init+0x1c/0x120
	[<c000000000007fbc>] .ret_from_kernel_thread+0x58/0x9c

Add a flag to msi_bitmap for tracking allocations from slab and memblock
so we can properly free/handle memory in msi_bitmap_free().
Signed-off-by: default avatarDenis Kirjanov <kda@linux-powerpc.org>
Reviewed-by: default avatarCatalin Marinas <catalin.marinas@arm.com>
[mpe: Reword changelog & use bitmap_from_slab in the if]
Signed-off-by: default avatarMichael Ellerman <mpe@ellerman.id.au>
parent 06bacefc
...@@ -19,6 +19,7 @@ struct msi_bitmap { ...@@ -19,6 +19,7 @@ struct msi_bitmap {
unsigned long *bitmap; unsigned long *bitmap;
spinlock_t lock; spinlock_t lock;
unsigned int irq_count; unsigned int irq_count;
bool bitmap_from_slab;
}; };
int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num); int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num);
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/bitmap.h> #include <linux/bitmap.h>
#include <linux/bootmem.h>
#include <asm/msi_bitmap.h> #include <asm/msi_bitmap.h>
#include <asm/setup.h> #include <asm/setup.h>
...@@ -122,7 +123,15 @@ int msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count, ...@@ -122,7 +123,15 @@ int msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
size = BITS_TO_LONGS(irq_count) * sizeof(long); size = BITS_TO_LONGS(irq_count) * sizeof(long);
pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size); pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size);
bmp->bitmap = zalloc_maybe_bootmem(size, GFP_KERNEL); bmp->bitmap_from_slab = slab_is_available();
if (bmp->bitmap_from_slab)
bmp->bitmap = kzalloc(size, GFP_KERNEL);
else {
bmp->bitmap = memblock_virt_alloc(size, 0);
/* the bitmap won't be freed from memblock allocator */
kmemleak_not_leak(bmp->bitmap);
}
if (!bmp->bitmap) { if (!bmp->bitmap) {
pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n"); pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n");
return -ENOMEM; return -ENOMEM;
...@@ -138,7 +147,8 @@ int msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count, ...@@ -138,7 +147,8 @@ int msi_bitmap_alloc(struct msi_bitmap *bmp, unsigned int irq_count,
void msi_bitmap_free(struct msi_bitmap *bmp) void msi_bitmap_free(struct msi_bitmap *bmp)
{ {
/* we can't free the bitmap we don't know if it's bootmem etc. */ if (bmp->bitmap_from_slab)
kfree(bmp->bitmap);
of_node_put(bmp->of_node); of_node_put(bmp->of_node);
bmp->bitmap = NULL; bmp->bitmap = NULL;
} }
...@@ -203,8 +213,6 @@ static void __init test_basics(void) ...@@ -203,8 +213,6 @@ static void __init test_basics(void)
/* Clients may WARN_ON bitmap == NULL for "not-allocated" */ /* Clients may WARN_ON bitmap == NULL for "not-allocated" */
WARN_ON(bmp.bitmap != NULL); WARN_ON(bmp.bitmap != NULL);
kfree(bmp.bitmap);
} }
static void __init test_of_node(void) static void __init test_of_node(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment