summaryrefslogtreecommitdiff
path: root/drivers/misc/tegra-profiler/mmap.c
blob: 9e3e05f305a561540f05f3af4412c9d6124fdcd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * drivers/misc/tegra-profiler/mmap.c
 *
 * Copyright (c) 2015, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/sched.h>

#include <linux/tegra_profiler.h>

#include "mmap.h"
#include "comm.h"
#include "hrt.h"

static void
put_mmap_sample(struct quadd_mmap_data *s, char *filename,
		size_t length, unsigned long pgoff, int is_file_exists)
{
	u64 mmap_ed = 0;
	struct quadd_record_data r;
	struct quadd_iovec vec[3];
	u64 pgoff_val = (u64)pgoff << PAGE_SHIFT;

	r.record_type = QUADD_RECORD_TYPE_MMAP;

	memcpy(&r.mmap, s, sizeof(*s));
	r.mmap.filename_length = length;

	if (is_file_exists)
		mmap_ed |= QUADD_MMAP_ED_IS_FILE_EXISTS;

	vec[0].base = &pgoff_val;
	vec[0].len = sizeof(pgoff_val);

	vec[1].base = &mmap_ed;
	vec[1].len = sizeof(mmap_ed);

	vec[2].base = filename;
	vec[2].len = length;

	pr_debug("MMAP: pid: %u, file_name: '%s', addr: %#llx - %#llx, len: %llx, pgoff: %#llx\n",
		 s->pid, filename,
		 s->addr, s->addr + s->len, s->len, pgoff_val);

	quadd_put_sample(&r, vec, ARRAY_SIZE(vec));
}

void quadd_process_mmap(struct vm_area_struct *vma, pid_t pid)
{
	int is_file_exists;
	struct file *vm_file;
	struct path *path;
	char *file_name, *tmp_buf = NULL;
	struct quadd_mmap_data sample;
	size_t length, length_aligned;

	if (!vma)
		return;

	if (!(vma->vm_flags & VM_EXEC))
		return;

	tmp_buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
	if (!tmp_buf)
		return;

	vm_file = vma->vm_file;
	if (vm_file) {
		path = &vm_file->f_path;

		file_name = d_path(path, tmp_buf, PATH_MAX);
		if (IS_ERR(file_name))
			goto out;

		length = strlen(file_name) + 1;
		is_file_exists = 1;
	} else {
		const char *name = NULL;

		name = arch_vma_name(vma);
		if (!name) {
			struct mm_struct *mm = vma->vm_mm;

			if (!mm) {
				name = "[vdso]";
			} else if (vma->vm_start <= mm->start_brk &&
				   vma->vm_end >= mm->brk) {
				name = "[heap]";
			} else if (vma->vm_start <= mm->start_stack &&
				   vma->vm_end >= mm->start_stack) {
				name = "[stack]";
			}
		}

		if (name)
			strcpy(tmp_buf, name);
		else
			sprintf(tmp_buf, "[vma:%08lx-%08lx]",
				vma->vm_start, vma->vm_end);

		file_name = tmp_buf;
		length = strlen(file_name) + 1;

		is_file_exists = 0;
	}

	length_aligned = ALIGN(length, sizeof(u64));

	sample.pid = pid;
	sample.user_mode = 1;

	sample.addr = vma->vm_start;
	sample.len = vma->vm_end - vma->vm_start;

	put_mmap_sample(&sample, file_name, length_aligned,
			vma->vm_pgoff, is_file_exists);

out:
	kfree(tmp_buf);
}

int quadd_get_current_mmap(pid_t pid)
{
	int is_file_exists;
	struct vm_area_struct *vma;
	struct file *vm_file;
	struct path *path;
	char *file_name;
	struct task_struct *task;
	struct mm_struct *mm;
	struct quadd_mmap_data sample;
	size_t length, length_aligned;
	char *tmp_buf;

	rcu_read_lock();
	task = pid_task(find_vpid(pid), PIDTYPE_PID);
	rcu_read_unlock();
	if (!task) {
		pr_err("Process not found: %d\n", pid);
		return -ESRCH;
	}

	mm = task->mm;
	if (!mm) {
		pr_warn("mm is not existed for task: %d\n", pid);
		return 0;
	}

	pr_info("Get mapped memory objects\n");

	tmp_buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
	if (!tmp_buf)
		return -ENOMEM;

	for (vma = mm->mmap; vma; vma = vma->vm_next) {
		if (!(vma->vm_flags & VM_EXEC))
			continue;

		vm_file = vma->vm_file;
		if (vm_file) {
			path = &vm_file->f_path;

			file_name = d_path(path, tmp_buf, PATH_MAX);
			if (IS_ERR(file_name))
				continue;

			length = strlen(file_name) + 1;
			is_file_exists = 1;
		} else {
			const char *name = NULL;

			name = arch_vma_name(vma);
			if (!name) {
				mm = vma->vm_mm;

				if (!mm) {
					name = "[vdso]";
				} else if (vma->vm_start <= mm->start_brk &&
					   vma->vm_end >= mm->brk) {
					name = "[heap]";
				} else if (vma->vm_start <= mm->start_stack &&
					   vma->vm_end >= mm->start_stack) {
					name = "[stack]";
				}
			}

			if (name)
				strcpy(tmp_buf, name);
			else
				sprintf(tmp_buf, "[vma:%08lx-%08lx]",
					vma->vm_start, vma->vm_end);

			file_name = tmp_buf;
			length = strlen(file_name) + 1;

			is_file_exists = 0;
		}

		length_aligned = ALIGN(length, sizeof(u64));

		sample.pid = pid;
		sample.user_mode = 1;

		sample.addr = vma->vm_start;
		sample.len = vma->vm_end - vma->vm_start;

		put_mmap_sample(&sample, file_name, length_aligned,
				vma->vm_pgoff, is_file_exists);
	}

	kfree(tmp_buf);

	return 0;
}