1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/kernel/profile.c 4 * Simple profiling. Manages a direct-mapped profile hit count buffer, 5 * with configurable resolution, support for restricting the cpus on 6 * which profiling is done, and switching between cpu time and 7 * schedule() calls via kernel command line parameters passed at boot. 8 * 9 * Scheduler profiling support, Arjan van de Ven and Ingo Molnar, 10 * Red Hat, July 2004 11 * Consolidation of architecture support code for profiling, 12 * Nadia Yvette Chambers, Oracle, July 2004 13 * Amortized hit count accounting via per-cpu open-addressed hashtables 14 * to resolve timer interrupt livelocks, Nadia Yvette Chambers, 15 * Oracle, 2004 16 */ 17 18 #include <linux/export.h> 19 #include <linux/profile.h> 20 #include <linux/memblock.h> 21 #include <linux/notifier.h> 22 #include <linux/mm.h> 23 #include <linux/cpumask.h> 24 #include <linux/cpu.h> 25 #include <linux/highmem.h> 26 #include <linux/mutex.h> 27 #include <linux/slab.h> 28 #include <linux/vmalloc.h> 29 #include <linux/sched/stat.h> 30 31 #include <asm/sections.h> 32 #include <asm/irq_regs.h> 33 #include <asm/ptrace.h> 34 35 struct profile_hit { 36 u32 pc, hits; 37 }; 38 #define PROFILE_GRPSHIFT 3 39 #define PROFILE_GRPSZ (1 << PROFILE_GRPSHIFT) 40 #define NR_PROFILE_HIT (PAGE_SIZE/sizeof(struct profile_hit)) 41 #define NR_PROFILE_GRP (NR_PROFILE_HIT/PROFILE_GRPSZ) 42 43 static atomic_t *prof_buffer; 44 static unsigned long prof_len; 45 static unsigned short int prof_shift; 46 47 int prof_on __read_mostly; 48 EXPORT_SYMBOL_GPL(prof_on); 49 50 int profile_setup(char *str) 51 { 52 static const char schedstr[] = "schedule"; 53 static const char sleepstr[] = "sleep"; 54 static const char kvmstr[] = "kvm"; 55 const char *select = NULL; 56 int par; 57 58 if (!strncmp(str, sleepstr, strlen(sleepstr))) { 59 #ifdef CONFIG_SCHEDSTATS 60 force_schedstat_enabled(); 61 prof_on = SLEEP_PROFILING; 62 select = sleepstr; 63 #else 64 pr_warn("kernel sleep profiling requires CONFIG_SCHEDSTATS\n"); 65 #endif /* CONFIG_SCHEDSTATS */ 66 } else if (!strncmp(str, schedstr, strlen(schedstr))) { 67 prof_on = SCHED_PROFILING; 68 select = schedstr; 69 } else if (!strncmp(str, kvmstr, strlen(kvmstr))) { 70 prof_on = KVM_PROFILING; 71 select = kvmstr; 72 } else if (get_option(&str, &par)) { 73 prof_shift = clamp(par, 0, BITS_PER_LONG - 1); 74 prof_on = CPU_PROFILING; 75 pr_info("kernel profiling enabled (shift: %u)\n", 76 prof_shift); 77 } 78 79 if (select) { 80 if (str[strlen(select)] == ',') 81 str += strlen(select) + 1; 82 if (get_option(&str, &par)) 83 prof_shift = clamp(par, 0, BITS_PER_LONG - 1); 84 pr_info("kernel %s profiling enabled (shift: %u)\n", 85 select, prof_shift); 86 } 87 88 return 1; 89 } 90 __setup("profile=", profile_setup); 91 92 93 int __ref profile_init(void) 94 { 95 int buffer_bytes; 96 if (!prof_on) 97 return 0; 98 99 /* only text is profiled */ 100 prof_len = (_etext - _stext) >> prof_shift; 101 102 if (!prof_len) { 103 pr_warn("profiling shift: %u too large\n", prof_shift); 104 prof_on = 0; 105 return -EINVAL; 106 } 107 108 buffer_bytes = prof_len*sizeof(atomic_t); 109 110 prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN); 111 if (prof_buffer) 112 return 0; 113 114 prof_buffer = alloc_pages_exact(buffer_bytes, 115 GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN); 116 if (prof_buffer) 117 return 0; 118 119 prof_buffer = vzalloc(buffer_bytes); 120 if (prof_buffer) 121 return 0; 122 123 return -ENOMEM; 124 } 125 126 static void do_profile_hits(int type, void *__pc, unsigned int nr_hits) 127 { 128 unsigned long pc; 129 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift; 130 if (pc < prof_len) 131 atomic_add(nr_hits, &prof_buffer[pc]); 132 } 133 134 void profile_hits(int type, void *__pc, unsigned int nr_hits) 135 { 136 if (prof_on != type || !prof_buffer) 137 return; 138 do_profile_hits(type, __pc, nr_hits); 139 } 140 EXPORT_SYMBOL_GPL(profile_hits); 141 142 void profile_tick(int type) 143 { 144 struct pt_regs *regs = get_irq_regs(); 145 146 /* This is the old kernel-only legacy profiling */ 147 if (!user_mode(regs)) 148 profile_hit(type, (void *)profile_pc(regs)); 149 } 150 151 #ifdef CONFIG_PROC_FS 152 #include <linux/proc_fs.h> 153 #include <linux/seq_file.h> 154 #include <linux/uaccess.h> 155 156 /* 157 * This function accesses profiling information. The returned data is 158 * binary: the sampling step and the actual contents of the profile 159 * buffer. Use of the program readprofile is recommended in order to 160 * get meaningful info out of these data. 161 */ 162 static ssize_t 163 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos) 164 { 165 unsigned long p = *ppos; 166 ssize_t read; 167 char *pnt; 168 unsigned long sample_step = 1UL << prof_shift; 169 170 if (p >= (prof_len+1)*sizeof(unsigned int)) 171 return 0; 172 if (count > (prof_len+1)*sizeof(unsigned int) - p) 173 count = (prof_len+1)*sizeof(unsigned int) - p; 174 read = 0; 175 176 while (p < sizeof(unsigned int) && count > 0) { 177 if (put_user(*((char *)(&sample_step)+p), buf)) 178 return -EFAULT; 179 buf++; p++; count--; read++; 180 } 181 pnt = (char *)prof_buffer + p - sizeof(atomic_t); 182 if (copy_to_user(buf, (void *)pnt, count)) 183 return -EFAULT; 184 read += count; 185 *ppos += read; 186 return read; 187 } 188 189 /* default is to not implement this call */ 190 int __weak setup_profiling_timer(unsigned mult) 191 { 192 return -EINVAL; 193 } 194 195 /* 196 * Writing to /proc/profile resets the counters 197 * 198 * Writing a 'profiling multiplier' value into it also re-sets the profiling 199 * interrupt frequency, on architectures that support this. 200 */ 201 static ssize_t write_profile(struct file *file, const char __user *buf, 202 size_t count, loff_t *ppos) 203 { 204 #ifdef CONFIG_SMP 205 if (count == sizeof(int)) { 206 unsigned int multiplier; 207 208 if (copy_from_user(&multiplier, buf, sizeof(int))) 209 return -EFAULT; 210 211 if (setup_profiling_timer(multiplier)) 212 return -EINVAL; 213 } 214 #endif 215 memset(prof_buffer, 0, prof_len * sizeof(atomic_t)); 216 return count; 217 } 218 219 static const struct proc_ops profile_proc_ops = { 220 .proc_read = read_profile, 221 .proc_write = write_profile, 222 .proc_lseek = default_llseek, 223 }; 224 225 int __ref create_proc_profile(void) 226 { 227 struct proc_dir_entry *entry; 228 int err = 0; 229 230 if (!prof_on) 231 return 0; 232 entry = proc_create("profile", S_IWUSR | S_IRUGO, 233 NULL, &profile_proc_ops); 234 if (entry) 235 proc_set_size(entry, (1 + prof_len) * sizeof(atomic_t)); 236 return err; 237 } 238 subsys_initcall(create_proc_profile); 239 #endif /* CONFIG_PROC_FS */ 240