diff mbox

[3/3] vmalloc: Add debugfs modfraginfo

Message ID 1530923744-25687-4-git-send-email-rick.p.edgecombe@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Edgecombe, Rick P July 7, 2018, 12:35 a.m. UTC
Add debugfs file "modfraginfo" for providing info on module space
fragmentation.  This can be used for determining if loadable module
randomization is causing any problems for extreme module loading situations,
like huge numbers of modules or extremely large modules.

Sample output when RANDOMIZE_BASE and X86_64 is configured:
Largest free space:		847253504
External Memory Fragementation:	20%
Allocations in backup area:	0

Sample output when just X86_64:
Largest free space:		847253504
External Memory Fragementation:	20%

Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
---
 mm/vmalloc.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 100 insertions(+), 1 deletion(-)

Comments

kernel test robot July 19, 2018, 5:24 a.m. UTC | #1
Hi Rick,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v4.18-rc5]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Rick-Edgecombe/KASLR-feature-to-randomize-each-loadable-module/20180707-083637
config: um-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=um 

All errors (new ones prefixed by >>):

   mm/vmalloc.c: In function 'modulefraginfo_debug_show':
>> mm/vmalloc.c:2964:41: error: 'MODULES_RAND_LEN' undeclared (first use in this function); did you mean 'MODULE_NAME_LEN'?
       if (obj->va_start >= MODULES_VADDR + MODULES_RAND_LEN)
                                            ^~~~~~~~~~~~~~~~
                                            MODULE_NAME_LEN
   mm/vmalloc.c:2964:41: note: each undeclared identifier is reported only once for each function it appears in

vim +2964 mm/vmalloc.c

  2933	
  2934	#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_64)
  2935	#if defined(CONFIG_RANDOMIZE_BASE)
  2936	static void print_backup_area(struct seq_file *m, unsigned long backup_cnt)
  2937	{
  2938		if (kaslr_enabled())
  2939			seq_printf(m, "Allocations in backup area:\t%lu\n", backup_cnt);
  2940	}
  2941	#else
  2942	static void print_backup_area(struct seq_file *m, unsigned long backup_cnt)
  2943	{
  2944	}
  2945	#endif
  2946	static int modulefraginfo_debug_show(struct seq_file *m, void *v)
  2947	{
  2948		struct list_head *i;
  2949		unsigned long last_end = MODULES_VADDR;
  2950		unsigned long total_free = 0;
  2951		unsigned long largest_free = 0;
  2952		unsigned long backup_cnt = 0;
  2953		unsigned long gap;
  2954	
  2955		spin_lock(&vmap_area_lock);
  2956	
  2957		list_for_each(i, &vmap_area_list) {
  2958			struct vmap_area *obj = list_entry(i, struct vmap_area, list);
  2959	
  2960			if (!(obj->flags & VM_LAZY_FREE)
  2961				&& obj->va_start >= MODULES_VADDR
  2962				&& obj->va_end <= MODULES_END) {
  2963	
> 2964				if (obj->va_start >= MODULES_VADDR + MODULES_RAND_LEN)
  2965					backup_cnt++;
  2966	
  2967				gap = (obj->va_start - last_end);
  2968				if (gap > largest_free)
  2969					largest_free = gap;
  2970				total_free += gap;
  2971	
  2972				last_end = obj->va_end;
  2973			}
  2974		}
  2975	
  2976		gap = (MODULES_END - last_end);
  2977		if (gap > largest_free)
  2978			largest_free = gap;
  2979		total_free += gap;
  2980	
  2981		spin_unlock(&vmap_area_lock);
  2982	
  2983		seq_printf(m, "Largest free space:\t\t%lu\n", largest_free);
  2984		if (total_free)
  2985			seq_printf(m, "External Memory Fragmentation:\t%lu%%\n",
  2986				100-(100*largest_free/total_free));
  2987		else
  2988			seq_puts(m, "External Memory Fragmentation:\t0%%\n");
  2989	
  2990		print_backup_area(m, backup_cnt);
  2991	
  2992		return 0;
  2993	}
  2994	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
diff mbox

Patch

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index b6f2449..85441f2 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -18,6 +18,7 @@ 
 #include <linux/interrupt.h>
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
+#include <linux/debugfs.h>
 #include <linux/debugobjects.h>
 #include <linux/kallsyms.h>
 #include <linux/list.h>
@@ -33,6 +34,7 @@ 
 #include <linux/bitops.h>
 
 #include <linux/uaccess.h>
+#include <asm/setup.h>
 #include <asm/tlbflush.h>
 #include <asm/shmparam.h>
 
@@ -2922,7 +2924,104 @@  static int __init proc_vmalloc_init(void)
 		proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op);
 	return 0;
 }
-module_init(proc_vmalloc_init);
+#else
+static int proc_vmalloc_init(void)
+{
+	return 0;
+}
+#endif
 
+#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_64)
+#if defined(CONFIG_RANDOMIZE_BASE)
+static void print_backup_area(struct seq_file *m, unsigned long backup_cnt)
+{
+	if (kaslr_enabled())
+		seq_printf(m, "Allocations in backup area:\t%lu\n", backup_cnt);
+}
+#else
+static void print_backup_area(struct seq_file *m, unsigned long backup_cnt)
+{
+}
 #endif
+static int modulefraginfo_debug_show(struct seq_file *m, void *v)
+{
+	struct list_head *i;
+	unsigned long last_end = MODULES_VADDR;
+	unsigned long total_free = 0;
+	unsigned long largest_free = 0;
+	unsigned long backup_cnt = 0;
+	unsigned long gap;
+
+	spin_lock(&vmap_area_lock);
+
+	list_for_each(i, &vmap_area_list) {
+		struct vmap_area *obj = list_entry(i, struct vmap_area, list);
+
+		if (!(obj->flags & VM_LAZY_FREE)
+			&& obj->va_start >= MODULES_VADDR
+			&& obj->va_end <= MODULES_END) {
+
+			if (obj->va_start >= MODULES_VADDR + MODULES_RAND_LEN)
+				backup_cnt++;
+
+			gap = (obj->va_start - last_end);
+			if (gap > largest_free)
+				largest_free = gap;
+			total_free += gap;
+
+			last_end = obj->va_end;
+		}
+	}
+
+	gap = (MODULES_END - last_end);
+	if (gap > largest_free)
+		largest_free = gap;
+	total_free += gap;
+
+	spin_unlock(&vmap_area_lock);
 
+	seq_printf(m, "Largest free space:\t\t%lu\n", largest_free);
+	if (total_free)
+		seq_printf(m, "External Memory Fragmentation:\t%lu%%\n",
+			100-(100*largest_free/total_free));
+	else
+		seq_puts(m, "External Memory Fragmentation:\t0%%\n");
+
+	print_backup_area(m, backup_cnt);
+
+	return 0;
+}
+
+static int proc_module_frag_debug_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, modulefraginfo_debug_show, NULL);
+}
+
+static const struct file_operations debug_module_frag_operations = {
+	.open       = proc_module_frag_debug_open,
+	.read       = seq_read,
+	.llseek     = seq_lseek,
+	.release    = single_release,
+};
+
+static void debug_modfrag_init(void)
+{
+	debugfs_create_file("modfraginfo", 0400, NULL, NULL,
+			&debug_module_frag_operations);
+}
+#else /* defined(CONFIG_DEBUG_FS) && defined(CONFIG_X86_64) */
+static void debug_modfrag_init(void)
+{
+}
+#endif
+
+#if defined(CONFIG_DEBUG_FS) || defined(CONFIG_PROC_FS)
+static int __init info_vmalloc_init(void)
+{
+	proc_vmalloc_init();
+	debug_modfrag_init();
+	return 0;
+}
+
+module_init(info_vmalloc_init);
+#endif