Skip to contentSkip to conversation

megaraid: kmemleak: Track page allocation for fusion

PATCH Superseded [email protected] 4 participants 4 messages Sep 14, 2017

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 megaraid: kmemleak: Track page allocation for fusion

From: Shu Wang <[email protected]>

Kmemleak reports about a thousand false positives for fusion-> cmd_list[]. Root casue is the cmd_list objects are allocated from slab allocator, and stored its pointer in object allocated by page allocator. The fix will tell kmemleak to track and scan fusion object.

Before patch:
kmemleak: 1004 new suspected memory leaks (see /sys/kernel/debug/kmemleak)

unreferenced object 0xffff88042584e000 (size 8192):
backtrace:

     kmemleak_alloc+0x4a/0xa0
     __kmalloc+0xec/0x220
     megasas_alloc_cmdlist_fusion+0x3e/0x140 [megaraid_sas]
     megasas_alloc_cmds_fusion+0x44/0x450 [megaraid_sas]
     megasas_init_adapter_fusion+0x21d/0x6e0 [megaraid_sas]
     megasas_init_fw+0x357/0xd30 [megaraid_sas]
     megasas_probe_one.part.34+0x5be/0x1040 [megaraid_sas]
     megasas_probe_one+0x46/0xc0 [megaraid_sas]
     local_pci_probe+0x45/0xa0
     work_for_cpu_fn+0x14/0x20
     process_one_work+0x149/0x360
     worker_thread+0x1d8/0x3c0
     kthread+0x109/0x140
     ret_from_fork+0x25/0x30
---
 drivers/scsi/megaraid/megaraid_sas_fusion.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/scsi/megaraid/megaraid_sas_fusion.c +7 -2
index 11bd2e698b84..621299edd8de 100644
@@ -48,6 +48,7 @@
 #include <linux/mutex.h>
 #include <linux/poll.h>
 #include <linux/vmalloc.h>
+#include <linux/kmemleak.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>
@@ -4512,7 +4513,9 @@ megasas_alloc_fusion_context(struct megasas_instance *instance)
 			dev_err(&instance->pdev->dev, "Failed from %s %d\n", __func__, __LINE__);
 			return -ENOMEM;
 		}
-	}
+	} else
+		kmemleak_alloc(instance->ctrl_context,
+			sizeof(struct fusion_context), 1, GFP_KERNEL);
 
 	fusion = instance->ctrl_context;
 
@@ -4548,9 +4551,11 @@ megasas_free_fusion_context(struct megasas_instance *instance)
 
 		if (is_vmalloc_addr(fusion))
 			vfree(fusion);
-		else
+		else {
 			free_pages((ulong)fusion,
 				instance->ctrl_context_pages);
+			kmemleak_free(fusion);
+		}
 	}
 }
 
Signature · 1 line
2.13.5
#1 lore raw
Quoted 13 lines · On Thu, 2017-09-14 at 14:16 +0800, [email protected] wrote:
drivers/scsi/megaraid/megaraid_sas_fusion.c +1 -0
index 11bd2e698b84..621299edd8de 100644
@@ -48,6 +48,7 @@
 #include <linux/mutex.h>
 #include <linux/poll.h>
 #include <linux/vmalloc.h>
+#include <linux/kmemleak.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>

drivers/scsi/megaraid/megaraid_sas_fusion.c megasas_alloc_fusion_context(struct megasas_instance *instance) · line 4520

Quoted 9 lines
drivers/scsi/megaraid/megaraid_sas_fusion.c +3 -1
@@ -4512,7 +4513,9 @@ megasas_alloc_fusion_context(struct megasas_instance *instance)
 			dev_err(&instance->pdev->dev, "Failed from %s %d\n", __func__, __LINE__);
 			return -ENOMEM;
 		}
-	}
+	} else
+		kmemleak_alloc(instance->ctrl_context,
+			sizeof(struct fusion_context), 1, GFP_KERNEL);
 
 	fusion = instance->ctrl_context;

Thank you for having tracked this down and for having shared this patch. But please keep braces balanced to keep checkpatch happy and please also consider adding a comment similar to the following above the kmemleak_alloc() call:

/* Allow kmemleak to scan these pages as they contain pointers to additional allocations. */

drivers/scsi/megaraid/megaraid_sas_fusion.c megasas_free_fusion_context(struct megasas_instance *instance) · line 4559

Quoted 11 lines
drivers/scsi/megaraid/megaraid_sas_fusion.c +3 -1
@@ -4548,9 +4551,11 @@ megasas_free_fusion_context(struct megasas_instance *instance)
 
 		if (is_vmalloc_addr(fusion))
 			vfree(fusion);
-		else
+		else {
 			free_pages((ulong)fusion,
 				instance->ctrl_context_pages);
+			kmemleak_free(fusion);
+		}
 	}

Please keep braces balanced in the above code too.

Thanks,

Bart.

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to [email protected]

-----Original Message-----
From: [email protected] [mailto:[email protected]]
Sent: Thursday, September 14, 2017 11:47 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Shu Wang
Subject: [PATCH] megaraid: kmemleak: Track page allocation for fusion

From: Shu Wang <[email protected]>

Kmemleak reports about a thousand false positives for fusion-> cmd_list[]. Root casue is the cmd_list objects are allocated from slab allocator, and stored its pointer in object allocated by page allocator. The fix will tell kmemleak to track and scan fusion object.

Before patch:
kmemleak: 1004 new suspected memory leaks (see /sys/kernel/debug/kmemleak)

unreferenced object 0xffff88042584e000 (size 8192):
backtrace:

     kmemleak_alloc+0x4a/0xa0
     __kmalloc+0xec/0x220
     megasas_alloc_cmdlist_fusion+0x3e/0x140 [megaraid_sas]
     megasas_alloc_cmds_fusion+0x44/0x450 [megaraid_sas]
     megasas_init_adapter_fusion+0x21d/0x6e0 [megaraid_sas]
     megasas_init_fw+0x357/0xd30 [megaraid_sas]
     megasas_probe_one.part.34+0x5be/0x1040 [megaraid_sas]
     megasas_probe_one+0x46/0xc0 [megaraid_sas]
     local_pci_probe+0x45/0xa0
     work_for_cpu_fn+0x14/0x20
     process_one_work+0x149/0x360
     worker_thread+0x1d8/0x3c0
     kthread+0x109/0x140
     ret_from_fork+0x25/0x30
---
 drivers/scsi/megaraid/megaraid_sas_fusion.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/scsi/megaraid/megaraid_sas_fusion.c
index 11bd2e698b84..621299edd8de 100644

drivers/scsi/megaraid/megaraid_sas_fusion.c +1 -0
@@ -48,6 +48,7 @@
 #include <linux/mutex.h>
 #include <linux/poll.h>
 #include <linux/vmalloc.h>
+#include <linux/kmemleak.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>
@@ -4512,7 +4513,9 @@ megasas_alloc_fusion_context(struct megasas_instance

*instance)
dev_err(&instance->pdev->dev, "Failed from %s
%d\n", __func__, __LINE__);
return -ENOMEM;
}
- }
+ } else
+ kmemleak_alloc(instance->ctrl_context,
+ sizeof(struct fusion_context), 1, GFP_KERNEL);

fusion = instance->ctrl_context;

@@ -4548,9 +4551,11 @@ megasas_free_fusion_context(struct megasas_instance *instance)

if (is_vmalloc_addr(fusion))
vfree(fusion);
- else
+ else {
free_pages((ulong)fusion,
instance->ctrl_context_pages);
+ kmemleak_free(fusion);
+ }
}
}
Looks good. Thanks for catching this.

--
2.13.5

Reviewed-by on v1 1/1
#3 lore raw
In reply to [email protected]
Quoted 57 lines · On Thu, Sep 14, 2017 at 02:16:46PM +0800, [email protected] wrote:

From: Shu Wang <[email protected]>

Kmemleak reports about a thousand false positives for fusion-> cmd_list[]. Root casue is the cmd_list objects are allocated from slab allocator, and stored its pointer in object allocated by page allocator. The fix will tell kmemleak to track and scan fusion object.

Before patch:
kmemleak: 1004 new suspected memory leaks (see /sys/kernel/debug/kmemleak)

unreferenced object 0xffff88042584e000 (size 8192):
backtrace:

     kmemleak_alloc+0x4a/0xa0
     __kmalloc+0xec/0x220
     megasas_alloc_cmdlist_fusion+0x3e/0x140 [megaraid_sas]
     megasas_alloc_cmds_fusion+0x44/0x450 [megaraid_sas]
     megasas_init_adapter_fusion+0x21d/0x6e0 [megaraid_sas]
     megasas_init_fw+0x357/0xd30 [megaraid_sas]
     megasas_probe_one.part.34+0x5be/0x1040 [megaraid_sas]
     megasas_probe_one+0x46/0xc0 [megaraid_sas]
     local_pci_probe+0x45/0xa0
     work_for_cpu_fn+0x14/0x20
     process_one_work+0x149/0x360
     worker_thread+0x1d8/0x3c0
     kthread+0x109/0x140
     ret_from_fork+0x25/0x30
---
 drivers/scsi/megaraid/megaraid_sas_fusion.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/scsi/megaraid/megaraid_sas_fusion.c +4 -1
index 11bd2e698b84..621299edd8de 100644
@@ -48,6 +48,7 @@
 #include <linux/mutex.h>
 #include <linux/poll.h>
 #include <linux/vmalloc.h>
+#include <linux/kmemleak.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>
@@ -4512,7 +4513,9 @@ megasas_alloc_fusion_context(struct megasas_instance *instance)
 			dev_err(&instance->pdev->dev, "Failed from %s %d\n", __func__, __LINE__);
 			return -ENOMEM;
 		}
-	}
+	} else
+		kmemleak_alloc(instance->ctrl_context,
+			sizeof(struct fusion_context), 1, GFP_KERNEL);
 
 	fusion = instance->ctrl_context;
 

drivers/scsi/megaraid/megaraid_sas_fusion.c megasas_free_fusion_context(struct megasas_instance *instance) · line 4558

Quoted 9 lines
drivers/scsi/megaraid/megaraid_sas_fusion.c +3 -1
@@ -4548,9 +4551,11 @@ megasas_free_fusion_context(struct megasas_instance *instance)
 
 		if (is_vmalloc_addr(fusion))
 			vfree(fusion);
-		else
+		else {
 			free_pages((ulong)fusion,
 				instance->ctrl_context_pages);
+			kmemleak_free(fusion);
+		}

Apart from Bart's comments on braces and comment before kmemleak_alloc(), I'd call kmemleak_free() before free_pages(), otherwise it may not interact nicely with other tools checking for use after free. With that:

Signature · 1 line
Catalin