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
profile_setup(char * str)50 int profile_setup(char *str)
51 {
52 static const char schedstr[] = "schedule";
53 static const char kvmstr[] = "kvm";
54 const char *select = NULL;
55 int par;
56
57 if (!strncmp(str, schedstr, strlen(schedstr))) {
58 prof_on = SCHED_PROFILING;
59 select = schedstr;
60 } else if (!strncmp(str, kvmstr, strlen(kvmstr))) {
61 prof_on = KVM_PROFILING;
62 select = kvmstr;
63 } else if (get_option(&str, &par)) {
64 prof_shift = clamp(par, 0, BITS_PER_LONG - 1);
65 prof_on = CPU_PROFILING;
66 pr_info("kernel profiling enabled (shift: %u)\n",
67 prof_shift);
68 }
69
70 if (select) {
71 if (str[strlen(select)] == ',')
72 str += strlen(select) + 1;
73 if (get_option(&str, &par))
74 prof_shift = clamp(par, 0, BITS_PER_LONG - 1);
75 pr_info("kernel %s profiling enabled (shift: %u)\n",
76 select, prof_shift);
77 }
78
79 return 1;
80 }
81 __setup("profile=", profile_setup);
82
83
profile_init(void)84 int __ref profile_init(void)
85 {
86 int buffer_bytes;
87 if (!prof_on)
88 return 0;
89
90 /* only text is profiled */
91 prof_len = (_etext - _stext) >> prof_shift;
92
93 if (!prof_len) {
94 pr_warn("profiling shift: %u too large\n", prof_shift);
95 prof_on = 0;
96 return -EINVAL;
97 }
98
99 buffer_bytes = prof_len*sizeof(atomic_t);
100
101 prof_buffer = kzalloc(buffer_bytes, GFP_KERNEL|__GFP_NOWARN);
102 if (prof_buffer)
103 return 0;
104
105 prof_buffer = alloc_pages_exact(buffer_bytes,
106 GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN);
107 if (prof_buffer)
108 return 0;
109
110 prof_buffer = vzalloc(buffer_bytes);
111 if (prof_buffer)
112 return 0;
113
114 return -ENOMEM;
115 }
116
117 /* Profile event notifications */
118
119 static BLOCKING_NOTIFIER_HEAD(task_exit_notifier);
120 static BLOCKING_NOTIFIER_HEAD(munmap_notifier);
121
profile_task_exit(struct task_struct * task)122 void profile_task_exit(struct task_struct *task)
123 {
124 blocking_notifier_call_chain(&task_exit_notifier, 0, task);
125 }
126
profile_munmap(unsigned long addr)127 void profile_munmap(unsigned long addr)
128 {
129 blocking_notifier_call_chain(&munmap_notifier, 0, (void *)addr);
130 }
131
profile_event_register(enum profile_type type,struct notifier_block * n)132 int profile_event_register(enum profile_type type, struct notifier_block *n)
133 {
134 int err = -EINVAL;
135
136 switch (type) {
137 case PROFILE_TASK_EXIT:
138 err = blocking_notifier_chain_register(
139 &task_exit_notifier, n);
140 break;
141 case PROFILE_MUNMAP:
142 err = blocking_notifier_chain_register(
143 &munmap_notifier, n);
144 break;
145 }
146
147 return err;
148 }
149 EXPORT_SYMBOL_GPL(profile_event_register);
150
profile_event_unregister(enum profile_type type,struct notifier_block * n)151 int profile_event_unregister(enum profile_type type, struct notifier_block *n)
152 {
153 int err = -EINVAL;
154
155 switch (type) {
156 case PROFILE_TASK_EXIT:
157 err = blocking_notifier_chain_unregister(
158 &task_exit_notifier, n);
159 break;
160 case PROFILE_MUNMAP:
161 err = blocking_notifier_chain_unregister(
162 &munmap_notifier, n);
163 break;
164 }
165
166 return err;
167 }
168 EXPORT_SYMBOL_GPL(profile_event_unregister);
169
do_profile_hits(int type,void * __pc,unsigned int nr_hits)170 static void do_profile_hits(int type, void *__pc, unsigned int nr_hits)
171 {
172 unsigned long pc;
173 pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
174 if (pc < prof_len)
175 atomic_add(nr_hits, &prof_buffer[pc]);
176 }
177
profile_hits(int type,void * __pc,unsigned int nr_hits)178 void profile_hits(int type, void *__pc, unsigned int nr_hits)
179 {
180 if (prof_on != type || !prof_buffer)
181 return;
182 do_profile_hits(type, __pc, nr_hits);
183 }
184 EXPORT_SYMBOL_GPL(profile_hits);
185
profile_tick(int type)186 void profile_tick(int type)
187 {
188 struct pt_regs *regs = get_irq_regs();
189
190 /* This is the old kernel-only legacy profiling */
191 if (!user_mode(regs))
192 profile_hit(type, (void *)profile_pc(regs));
193 }
194
195 #ifdef CONFIG_PROC_FS
196 #include <linux/proc_fs.h>
197 #include <linux/seq_file.h>
198 #include <linux/uaccess.h>
199
200 /*
201 * This function accesses profiling information. The returned data is
202 * binary: the sampling step and the actual contents of the profile
203 * buffer. Use of the program readprofile is recommended in order to
204 * get meaningful info out of these data.
205 */
206 static ssize_t
read_profile(struct file * file,char __user * buf,size_t count,loff_t * ppos)207 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
208 {
209 unsigned long p = *ppos;
210 ssize_t read;
211 char *pnt;
212 unsigned long sample_step = 1UL << prof_shift;
213
214 if (p >= (prof_len+1)*sizeof(unsigned int))
215 return 0;
216 if (count > (prof_len+1)*sizeof(unsigned int) - p)
217 count = (prof_len+1)*sizeof(unsigned int) - p;
218 read = 0;
219
220 while (p < sizeof(unsigned int) && count > 0) {
221 if (put_user(*((char *)(&sample_step)+p), buf))
222 return -EFAULT;
223 buf++; p++; count--; read++;
224 }
225 pnt = (char *)prof_buffer + p - sizeof(atomic_t);
226 if (copy_to_user(buf, (void *)pnt, count))
227 return -EFAULT;
228 read += count;
229 *ppos += read;
230 return read;
231 }
232
233 /* default is to not implement this call */
setup_profiling_timer(unsigned mult)234 int __weak setup_profiling_timer(unsigned mult)
235 {
236 return -EINVAL;
237 }
238
239 /*
240 * Writing to /proc/profile resets the counters
241 *
242 * Writing a 'profiling multiplier' value into it also re-sets the profiling
243 * interrupt frequency, on architectures that support this.
244 */
write_profile(struct file * file,const char __user * buf,size_t count,loff_t * ppos)245 static ssize_t write_profile(struct file *file, const char __user *buf,
246 size_t count, loff_t *ppos)
247 {
248 #ifdef CONFIG_SMP
249 if (count == sizeof(int)) {
250 unsigned int multiplier;
251
252 if (copy_from_user(&multiplier, buf, sizeof(int)))
253 return -EFAULT;
254
255 if (setup_profiling_timer(multiplier))
256 return -EINVAL;
257 }
258 #endif
259 memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
260 return count;
261 }
262
263 static const struct proc_ops profile_proc_ops = {
264 .proc_read = read_profile,
265 .proc_write = write_profile,
266 .proc_lseek = default_llseek,
267 };
268
create_proc_profile(void)269 int __ref create_proc_profile(void)
270 {
271 struct proc_dir_entry *entry;
272 int err = 0;
273
274 if (!prof_on)
275 return 0;
276 entry = proc_create("profile", S_IWUSR | S_IRUGO,
277 NULL, &profile_proc_ops);
278 if (entry)
279 proc_set_size(entry, (1 + prof_len) * sizeof(atomic_t));
280 return err;
281 }
282 subsys_initcall(create_proc_profile);
283 #endif /* CONFIG_PROC_FS */
284