Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: Denis Kirjanov <kda@linux-powerpc.org>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
[mpe: Reword changelog & use bitmap_from_slab in the if]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

authored by

Denis Kirjanov and committed by
Michael Ellerman
cb2d3883 06bacefc

+13 -4
+1
arch/powerpc/include/asm/msi_bitmap.h
··· 19 19 unsigned long *bitmap; 20 20 spinlock_t lock; 21 21 unsigned int irq_count; 22 + bool bitmap_from_slab; 22 23 }; 23 24 24 25 int msi_bitmap_alloc_hwirqs(struct msi_bitmap *bmp, int num);
+12 -4
arch/powerpc/sysdev/msi_bitmap.c
··· 11 11 #include <linux/slab.h> 12 12 #include <linux/kernel.h> 13 13 #include <linux/bitmap.h> 14 + #include <linux/bootmem.h> 14 15 #include <asm/msi_bitmap.h> 15 16 #include <asm/setup.h> 16 17 ··· 123 122 size = BITS_TO_LONGS(irq_count) * sizeof(long); 124 123 pr_debug("msi_bitmap: allocator bitmap size is 0x%x bytes\n", size); 125 124 126 - bmp->bitmap = zalloc_maybe_bootmem(size, GFP_KERNEL); 125 + bmp->bitmap_from_slab = slab_is_available(); 126 + if (bmp->bitmap_from_slab) 127 + bmp->bitmap = kzalloc(size, GFP_KERNEL); 128 + else { 129 + bmp->bitmap = memblock_virt_alloc(size, 0); 130 + /* the bitmap won't be freed from memblock allocator */ 131 + kmemleak_not_leak(bmp->bitmap); 132 + } 133 + 127 134 if (!bmp->bitmap) { 128 135 pr_debug("msi_bitmap: ENOMEM allocating allocator bitmap!\n"); 129 136 return -ENOMEM; ··· 147 138 148 139 void msi_bitmap_free(struct msi_bitmap *bmp) 149 140 { 150 - /* we can't free the bitmap we don't know if it's bootmem etc. */ 141 + if (bmp->bitmap_from_slab) 142 + kfree(bmp->bitmap); 151 143 of_node_put(bmp->of_node); 152 144 bmp->bitmap = NULL; 153 145 } ··· 213 203 214 204 /* Clients may WARN_ON bitmap == NULL for "not-allocated" */ 215 205 WARN_ON(bmp.bitmap != NULL); 216 - 217 - kfree(bmp.bitmap); 218 206 } 219 207 220 208 static void __init test_of_node(void)