Skip to content

Commit 877b911

Browse files
dcuiLorenzo Pieralisi
authored andcommitted
PCI: hv: Avoid a kmemleak false positive caused by the hbus buffer
With the recent 59bb479 ("mm, sl[aou]b: guarantee natural alignment for kmalloc(power-of-two)"), kzalloc() is able to allocate a 4KB buffer that is guaranteed to be 4KB-aligned. Here the size and alignment of hbus is important because hbus's field retarget_msi_interrupt_params must not cross a 4KB page boundary. Here we prefer kzalloc to get_zeroed_page(), because a buffer allocated by the latter is not tracked and scanned by kmemleak, and hence kmemleak reports the pointer contained in the hbus buffer (i.e. the hpdev struct, which is created in new_pcichild_device() and is tracked by hbus->children) as memory leak (false positive). If the kernel doesn't have 59bb479, get_zeroed_page() *must* be used to allocate the hbus buffer and we can avoid the kmemleak false positive by using kmemleak_alloc() and kmemleak_free() to ask kmemleak to track and scan the hbus buffer. Reported-by: Lili Deng <[email protected]> Signed-off-by: Dexuan Cui <[email protected]> Signed-off-by: Lorenzo Pieralisi <[email protected]> Reviewed-by: Michael Kelley <[email protected]>
1 parent 14ef39f commit 877b911

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

drivers/pci/controller/pci-hyperv.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,9 +2902,27 @@ static int hv_pci_probe(struct hv_device *hdev,
29022902
* hv_pcibus_device contains the hypercall arguments for retargeting in
29032903
* hv_irq_unmask(). Those must not cross a page boundary.
29042904
*/
2905-
BUILD_BUG_ON(sizeof(*hbus) > PAGE_SIZE);
2905+
BUILD_BUG_ON(sizeof(*hbus) > HV_HYP_PAGE_SIZE);
29062906

2907-
hbus = (struct hv_pcibus_device *)get_zeroed_page(GFP_KERNEL);
2907+
/*
2908+
* With the recent 59bb47985c1d ("mm, sl[aou]b: guarantee natural
2909+
* alignment for kmalloc(power-of-two)"), kzalloc() is able to allocate
2910+
* a 4KB buffer that is guaranteed to be 4KB-aligned. Here the size and
2911+
* alignment of hbus is important because hbus's field
2912+
* retarget_msi_interrupt_params must not cross a 4KB page boundary.
2913+
*
2914+
* Here we prefer kzalloc to get_zeroed_page(), because a buffer
2915+
* allocated by the latter is not tracked and scanned by kmemleak, and
2916+
* hence kmemleak reports the pointer contained in the hbus buffer
2917+
* (i.e. the hpdev struct, which is created in new_pcichild_device() and
2918+
* is tracked by hbus->children) as memory leak (false positive).
2919+
*
2920+
* If the kernel doesn't have 59bb47985c1d, get_zeroed_page() *must* be
2921+
* used to allocate the hbus buffer and we can avoid the kmemleak false
2922+
* positive by using kmemleak_alloc() and kmemleak_free() to ask
2923+
* kmemleak to track and scan the hbus buffer.
2924+
*/
2925+
hbus = (struct hv_pcibus_device *)kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
29082926
if (!hbus)
29092927
return -ENOMEM;
29102928
hbus->state = hv_pcibus_init;
@@ -3133,7 +3151,7 @@ static int hv_pci_remove(struct hv_device *hdev)
31333151

31343152
hv_put_dom_num(hbus->sysdata.domain);
31353153

3136-
free_page((unsigned long)hbus);
3154+
kfree(hbus);
31373155
return ret;
31383156
}
31393157

0 commit comments

Comments
 (0)