From d331fe655698659ee0aa6a01e77395bfd34de4a8 Mon Sep 17 00:00:00 2001 From: Suren Baghdasaryan Date: Sun, 31 Jul 2022 09:15:12 -0700 Subject: TESTING: drivers/staging: add module for testing code tagging Test: echo 0 > /proc/sys/kernel/panic_on_warn insmod ctagmod.ko echo 64 > /sys/kernel/mm/ctagmod/kmalloc_size echo 4097 > /sys/kernel/mm/ctagmod/pgalloc_size cat /proc/allocinfo rmmod ctagmod.ko <-- should generate a warning about leaks cat /proc/allocinfo Signed-off-by: Suren Baghdasaryan --- drivers/staging/Kconfig | 2 + drivers/staging/Makefile | 1 + drivers/staging/ctagmod/Kconfig | 10 ++++ drivers/staging/ctagmod/Makefile | 2 + drivers/staging/ctagmod/ctagmod.c | 115 ++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 9 +++ 6 files changed, 139 insertions(+) create mode 100644 drivers/staging/ctagmod/Kconfig create mode 100644 drivers/staging/ctagmod/Makefile create mode 100644 drivers/staging/ctagmod/ctagmod.c diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig index 784b9f673ead..0c303e5a0052 100644 --- a/drivers/staging/Kconfig +++ b/drivers/staging/Kconfig @@ -74,4 +74,6 @@ source "drivers/staging/fieldbus/Kconfig" source "drivers/staging/vme_user/Kconfig" +source "drivers/staging/ctagmod/Kconfig" + endif # STAGING diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile index 2ea99c7b05d9..35225df29842 100644 --- a/drivers/staging/Makefile +++ b/drivers/staging/Makefile @@ -26,3 +26,4 @@ obj-$(CONFIG_BCM2835_VCHIQ) += vc04_services/ obj-$(CONFIG_PI433) += pi433/ obj-$(CONFIG_XIL_AXIS_FIFO) += axis-fifo/ obj-$(CONFIG_FIELDBUS_DEV) += fieldbus/ +obj-$(CONFIG_CTAGMOD) += ctagmod/ diff --git a/drivers/staging/ctagmod/Kconfig b/drivers/staging/ctagmod/Kconfig new file mode 100644 index 000000000000..e66394482888 --- /dev/null +++ b/drivers/staging/ctagmod/Kconfig @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 +config CTAGMOD + tristate "Code tagging test module" + depends on CODE_TAGGING + default m + help + Code tagging test module. + + To compile this driver as a module, choose M here: the module + will be called ctagmod. diff --git a/drivers/staging/ctagmod/Makefile b/drivers/staging/ctagmod/Makefile new file mode 100644 index 000000000000..63342b42d82c --- /dev/null +++ b/drivers/staging/ctagmod/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_CTAGMOD) += ctagmod.o diff --git a/drivers/staging/ctagmod/ctagmod.c b/drivers/staging/ctagmod/ctagmod.c new file mode 100644 index 000000000000..af41dfbdd411 --- /dev/null +++ b/drivers/staging/ctagmod/ctagmod.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include + +MODULE_LICENSE("GPL"); + +static int kmalloc_sz; +static void *kmalloc_ptr; +static int pgalloc_sz; +static struct page *pgalloc_ptr; + +static ssize_t parse_size(const char *buf) +{ + unsigned long sz; + int err; + + err = kstrtoul(buf, 0, &sz); + if (err) + return err; + return sz; +} + +static ssize_t kmalloc_size_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) +{ + return sprintf(buf, "%d\n", kmalloc_sz); +} + +static ssize_t kmalloc_size_store(struct kobject *kobj, struct kobj_attribute *attr, + const char *buf, size_t count) +{ + ssize_t sz = parse_size(buf); + + if (sz < 0) + return sz; + + if (kmalloc_sz) + kfree(kmalloc_ptr); + if (sz > 0) { + kmalloc_ptr = kmalloc(sz, GFP_KERNEL); + if (unlikely(!kmalloc_ptr)) { + printk(KERN_ERR "kmalloc failed!\n"); + return -ENOMEM; + } + } + kmalloc_sz = sz; + + return count; +} + +static struct kobj_attribute dev_attr_kmalloc_size = __ATTR_RW_MODE(kmalloc_size, 0600); + +static ssize_t pgalloc_size_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) +{ + return sprintf(buf, "%d\n", pgalloc_sz); +} + +static ssize_t pgalloc_size_store(struct kobject *kobj, struct kobj_attribute *attr, + const char *buf, size_t count) +{ + ssize_t sz = parse_size(buf); + + if (sz < 0) + return sz; + + if (pgalloc_sz) + free_pages((unsigned long)page_address(pgalloc_ptr), get_order(pgalloc_sz)); + if (sz > 0) { + pgalloc_ptr = alloc_pages(GFP_KERNEL, get_order(sz)); + if (unlikely(!pgalloc_ptr)) { + printk(KERN_ERR "alloc_pages failed!\n"); + return -ENOMEM; + } + } + pgalloc_sz = sz; + + return count; +} + +static struct kobj_attribute dev_attr_pgalloc_size = __ATTR_RW_MODE(pgalloc_size, 0600); + +static struct attribute *ctagmod_attributes[] = { + &dev_attr_kmalloc_size.attr, + &dev_attr_pgalloc_size.attr, + NULL +}; + +static struct attribute_group ctagmod_attr_group = { + .name = "ctagmod", + .attrs = ctagmod_attributes, +}; + +static int __init ctagmod_start(void) +{ + printk(KERN_INFO "Loading ctagmod module\n"); +#ifdef CONFIG_MEM_ALLOC_PROFILING + if (sysfs_create_group(mm_kobj, &ctagmod_attr_group)) + pr_err("ctagmod: failed to create sysfs group\n"); +#else + printk(KERN_INFO "CONFIG_MEM_ALLOC_PROFILING is undefined\n"); +#endif + return 0; +} + +static void __exit ctagmod_end(void) +{ + printk(KERN_INFO "Unloading ctagmod module\n"); +#ifdef CONFIG_MEM_ALLOC_PROFILING + sysfs_remove_group(mm_kobj, &ctagmod_attr_group); +#endif +} + +module_init(ctagmod_start); +module_exit(ctagmod_end); diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 0e49984826a6..f1093c6f4843 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -998,6 +998,15 @@ config MEM_ALLOC_PROFILING_DEBUG Adds warnings with helpful error messages for memory allocation profiling. +config CODE_TAG_TESTING + bool "Enable code tag testing" + default y + select MEM_ALLOC_PROFILING + select MEM_ALLOC_PROFILING_DEBUG + select STAGING + help + Enable to test code tagging. + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan" -- cgit v1.2.3