xref: /linux/kernel/profile.c (revision b2db6f4ace72e71fa09b8d1354f8ac9854140d74)
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 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 
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 static void do_profile_hits(int type, void *__pc, unsigned int nr_hits)
118 {
119 	unsigned long pc;
120 	pc = ((unsigned long)__pc - (unsigned long)_stext) >> prof_shift;
121 	if (pc < prof_len)
122 		atomic_add(nr_hits, &prof_buffer[pc]);
123 }
124 
125 void profile_hits(int type, void *__pc, unsigned int nr_hits)
126 {
127 	if (prof_on != type || !prof_buffer)
128 		return;
129 	do_profile_hits(type, __pc, nr_hits);
130 }
131 EXPORT_SYMBOL_GPL(profile_hits);
132 
133 void profile_tick(int type)
134 {
135 	struct pt_regs *regs = get_irq_regs();
136 
137 	/* This is the old kernel-only legacy profiling */
138 	if (!user_mode(regs))
139 		profile_hit(type, (void *)profile_pc(regs));
140 }
141 
142 #ifdef CONFIG_PROC_FS
143 #include <linux/proc_fs.h>
144 #include <linux/seq_file.h>
145 #include <linux/uaccess.h>
146 
147 /*
148  * This function accesses profiling information. The returned data is
149  * binary: the sampling step and the actual contents of the profile
150  * buffer. Use of the program readprofile is recommended in order to
151  * get meaningful info out of these data.
152  */
153 static ssize_t
154 read_profile(struct file *file, char __user *buf, size_t count, loff_t *ppos)
155 {
156 	unsigned long p = *ppos;
157 	ssize_t read;
158 	char *pnt;
159 	unsigned long sample_step = 1UL << prof_shift;
160 
161 	if (p >= (prof_len+1)*sizeof(unsigned int))
162 		return 0;
163 	if (count > (prof_len+1)*sizeof(unsigned int) - p)
164 		count = (prof_len+1)*sizeof(unsigned int) - p;
165 	read = 0;
166 
167 	while (p < sizeof(unsigned int) && count > 0) {
168 		if (put_user(*((char *)(&sample_step)+p), buf))
169 			return -EFAULT;
170 		buf++; p++; count--; read++;
171 	}
172 	pnt = (char *)prof_buffer + p - sizeof(atomic_t);
173 	if (copy_to_user(buf, (void *)pnt, count))
174 		return -EFAULT;
175 	read += count;
176 	*ppos += read;
177 	return read;
178 }
179 
180 /* default is to not implement this call */
181 int __weak setup_profiling_timer(unsigned mult)
182 {
183 	return -EINVAL;
184 }
185 
186 /*
187  * Writing to /proc/profile resets the counters
188  *
189  * Writing a 'profiling multiplier' value into it also re-sets the profiling
190  * interrupt frequency, on architectures that support this.
191  */
192 static ssize_t write_profile(struct file *file, const char __user *buf,
193 			     size_t count, loff_t *ppos)
194 {
195 #ifdef CONFIG_SMP
196 	if (count == sizeof(int)) {
197 		unsigned int multiplier;
198 
199 		if (copy_from_user(&multiplier, buf, sizeof(int)))
200 			return -EFAULT;
201 
202 		if (setup_profiling_timer(multiplier))
203 			return -EINVAL;
204 	}
205 #endif
206 	memset(prof_buffer, 0, prof_len * sizeof(atomic_t));
207 	return count;
208 }
209 
210 static const struct proc_ops profile_proc_ops = {
211 	.proc_read	= read_profile,
212 	.proc_write	= write_profile,
213 	.proc_lseek	= default_llseek,
214 };
215 
216 int __ref create_proc_profile(void)
217 {
218 	struct proc_dir_entry *entry;
219 	int err = 0;
220 
221 	if (!prof_on)
222 		return 0;
223 	entry = proc_create("profile", S_IWUSR | S_IRUGO,
224 			    NULL, &profile_proc_ops);
225 	if (entry)
226 		proc_set_size(entry, (1 + prof_len) * sizeof(atomic_t));
227 	return err;
228 }
229 subsys_initcall(create_proc_profile);
230 #endif /* CONFIG_PROC_FS */
231