[PATCH v2 2/3] kmemleak: Add special scan test case

From: Hiroshi DOYU
Date: Tue Jun 01 2010 - 06:25:56 EST


From: Hiroshi DOYU <Hiroshi.DOYU@xxxxxxxxx>

For this test case, a pointer is converted to a physical address with
attribution bits. This test can be executed either with special scan
or without special scan. For special scan case, specifying the testing
period second with the kernel module parameter "timeout". Afther that
timeout, special scan is unregistered automatically. Without this
timeout, you will get false positives with kmemleak scan.

Signed-off-by: Hiroshi DOYU <Hiroshi.DOYU@xxxxxxxxx>
---
mm/Makefile | 2 +-
mm/kmemleak-special-test.c | 94 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 95 insertions(+), 1 deletions(-)
create mode 100644 mm/kmemleak-special-test.c

diff --git a/mm/Makefile b/mm/Makefile
index 8982504..5d08581 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -44,4 +44,4 @@ obj-$(CONFIG_CGROUP_MEM_RES_CTLR) += memcontrol.o page_cgroup.o
obj-$(CONFIG_MEMORY_FAILURE) += memory-failure.o
obj-$(CONFIG_HWPOISON_INJECT) += hwpoison-inject.o
obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o
-obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak-test.o
+obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak-test.o kmemleak-special-test.o
diff --git a/mm/kmemleak-special-test.c b/mm/kmemleak-special-test.c
new file mode 100644
index 0000000..ed6788c
--- /dev/null
+++ b/mm/kmemleak-special-test.c
@@ -0,0 +1,94 @@
+/*
+ * kmemleak: special scan test
+ *
+ * Copyright (C) 2010 Nokia Corporation
+ *
+ * Written by Hiroshi DOYU <Hiroshi.DOYU@xxxxxxxxx>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/module.h>
+#include <linux/kmemleak.h>
+
+static int timeout;
+module_param(timeout, int, 0644);
+MODULE_PARM_DESC(timeout, "special scan mode timeout");
+
+#define MAXELEM 8
+static u32 elem[MAXELEM];
+
+static void no_special_func(struct work_struct *unused)
+{
+ kmemleak_no_special(elem);
+ pr_info("%s: Unregistered special scan %p\n", __func__, elem);
+}
+static DECLARE_DELAYED_WORK(no_special_work, no_special_func);
+
+static unsigned long custom_conversion(void *unused, unsigned long orig)
+{
+ u32 addr = orig;
+
+ addr &= ~1;
+ addr = (u32)phys_to_virt(addr);
+
+ pr_info("%s: Converted %08lx -> %08x\n", __func__, orig, addr);
+
+ return addr;
+}
+
+static int __init kmemleak_special_init(void)
+{
+ int i, err;
+
+ for (i = 0; i < ARRAY_SIZE(elem); i++) {
+ void *virt;
+
+ virt = kmalloc(SZ_1K, GFP_KERNEL);
+ if (!virt)
+ continue;
+
+ memset(virt, 0xa5, SZ_1K); /* fill out some markers */
+ *(char *)virt = i;
+
+ elem[i] = virt_to_phys(virt) | 1;
+ pr_info("Stored %d@%p -> %08x\n", SZ_1K, virt, elem[i]);
+ }
+
+ if (!timeout)
+ return 0;
+
+ err = kmemleak_special_scan(elem, sizeof(elem),
+ custom_conversion, NULL, THIS_MODULE);
+ if (err) {
+ pr_warning("%s: Couldn't register special scan\n",
+ __func__);
+ return -ENOMEM;
+ }
+ pr_info("%s: Registered special scan: %p\n", __func__, elem);
+
+ schedule_delayed_work(&no_special_work,
+ msecs_to_jiffies(timeout * 1000));
+ return 0;
+}
+module_init(kmemleak_special_init);
+
+static void __exit kmemleak_special_exit(void)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(elem); i++) {
+ u32 val;
+
+ val = elem[i];
+ val &= ~1;
+ kfree(phys_to_virt(val));
+ }
+}
+module_exit(kmemleak_special_exit);
+
+MODULE_DESCRIPTION("kmemleak: special scan test");
+MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@xxxxxxxxx>");
+MODULE_LICENSE("GPL v2");
--
1.7.1.rc1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/