/*
* drivers/misc/tegra-profiler/mmap.c
*
- * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
+ * 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,
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
-#include <linux/module.h>
#include <linux/mm.h>
-#include <linux/crc32.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"
-#include "debug.h"
-static struct quadd_mmap_ctx mmap_ctx;
-
-static int binary_search_and_add(unsigned int *array,
- unsigned int length, unsigned int key)
+static void
+put_mmap_sample(struct quadd_mmap_data *s, char *filename,
+ size_t length, unsigned long pgoff, int is_file_exists)
{
- unsigned int i_min, i_max, mid;
+ u64 mmap_ed = 0;
+ struct quadd_record_data r;
+ struct quadd_iovec vec[3];
+ u64 pgoff_val = (u64)pgoff << PAGE_SHIFT;
- if (length == 0) {
- array[0] = key;
- return 1;
- } else if (length == 1 && array[0] == key) {
- return 0;
- }
+ r.record_type = QUADD_RECORD_TYPE_MMAP;
- i_min = 0;
- i_max = length;
-
- if (array[0] > key) {
- memmove((char *)((unsigned int *)array + 1), array,
- length * sizeof(unsigned int));
- array[0] = key;
- return 1;
- } else if (array[length - 1] < key) {
- array[length] = key;
- return 1;
- }
+ memcpy(&r.mmap, s, sizeof(*s));
+ r.mmap.filename_length = length;
- while (i_min < i_max) {
- mid = i_min + (i_max - i_min) / 2;
-
- if (key <= array[mid])
- i_max = mid;
- else
- i_min = mid + 1;
- }
+ if (is_file_exists)
+ mmap_ed |= QUADD_MMAP_ED_IS_FILE_EXISTS;
- if (array[i_max] == key) {
- return 0;
- } else {
- memmove((char *)((unsigned int *)array + i_max + 1),
- (char *)((unsigned int *)array + i_max),
- (length - i_max) * sizeof(unsigned int));
- array[i_max] = key;
- return 1;
- }
-}
+ vec[0].base = &pgoff_val;
+ vec[0].len = sizeof(pgoff_val);
-static int check_hash(u32 key)
-{
- int res;
- unsigned long flags;
+ vec[1].base = &mmap_ed;
+ vec[1].len = sizeof(mmap_ed);
- spin_lock_irqsave(&mmap_ctx.lock, flags);
+ vec[2].base = filename;
+ vec[2].len = length;
- if (mmap_ctx.nr_hashes >= QUADD_MMAP_SIZE_ARRAY) {
- spin_unlock_irqrestore(&mmap_ctx.lock, flags);
- return 1;
- }
+ 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);
- res = binary_search_and_add(mmap_ctx.hash_array,
- mmap_ctx.nr_hashes, key);
- if (res > 0) {
- mmap_ctx.nr_hashes++;
- spin_unlock_irqrestore(&mmap_ctx.lock, flags);
- return 0;
- }
-
- spin_unlock_irqrestore(&mmap_ctx.lock, flags);
- return 1;
+ quadd_put_sample(&r, vec, ARRAY_SIZE(vec));
}
-char *quadd_get_mmap(struct quadd_cpu_context *cpu_ctx,
- struct pt_regs *regs, struct quadd_mmap_data *sample,
- unsigned int *extra_length)
+void quadd_process_mmap(struct vm_area_struct *vma, pid_t pid)
{
- u32 crc;
- unsigned long ip;
- int length, length_aligned;
- struct mm_struct *mm = current->mm;
- struct vm_area_struct *vma;
+ int is_file_exists;
struct file *vm_file;
struct path *path;
- char *file_name = NULL;
- struct quadd_mmap_cpu_ctx *mm_cpu_ctx = this_cpu_ptr(mmap_ctx.cpu_ctx);
- char *tmp_buf = mm_cpu_ctx->tmp_buf;
+ char *file_name, *tmp_buf = NULL;
+ struct quadd_mmap_data sample;
+ size_t length, length_aligned;
- if (!mm) {
- *extra_length = 0;
- return NULL;
- }
+ if (!vma)
+ return;
- ip = instruction_pointer(regs);
+ if (!(vma->vm_flags & VM_EXEC))
+ return;
- if (user_mode(regs)) {
- for (vma = find_vma(mm, ip); vma; vma = vma->vm_next) {
- if (ip < vma->vm_start || ip >= vma->vm_end)
- continue;
+ tmp_buf = kzalloc(PATH_MAX + sizeof(u64), GFP_ATOMIC);
+ if (!tmp_buf)
+ return;
- vm_file = vma->vm_file;
- if (!vm_file)
- break;
+ vm_file = vma->vm_file;
+ if (vm_file) {
+ path = &vm_file->f_path;
- path = &vm_file->f_path;
+ file_name = d_path(path, tmp_buf, PATH_MAX);
+ if (IS_ERR(file_name))
+ goto out;
- file_name = d_path(path, tmp_buf, PATH_MAX);
- if (IS_ERR(file_name)) {
- file_name = NULL;
- } else {
- sample->addr = vma->vm_start;
- sample->len = vma->vm_end - vma->vm_start;
- sample->pgoff =
- (u64)vma->vm_pgoff << PAGE_SHIFT;
- }
- break;
- }
+ length = strlen(file_name) + 1;
+ is_file_exists = 1;
} else {
- struct module *mod;
-
- preempt_disable();
- mod = __module_address(ip);
- preempt_enable();
-
- if (mod) {
- file_name = mod->name;
- if (file_name) {
- sample->addr = (u32) mod->module_core;
- sample->len = mod->core_size;
- sample->pgoff = 0;
+ 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;
}
- if (file_name) {
- length = strlen(file_name);
- if (length >= PATH_MAX) {
- *extra_length = 0;
- return NULL;
- }
+ length_aligned = ALIGN(length, sizeof(u64));
- crc = crc32_le(~0, file_name, length);
- crc = crc32_le(crc, (unsigned char *)&sample->addr,
- sizeof(sample->addr));
- crc = crc32_le(crc, (unsigned char *)&sample->len,
- sizeof(sample->len));
+ sample.pid = pid;
+ sample.user_mode = 1;
- if (!check_hash(crc)) {
- strcpy(cpu_ctx->mmap_filename, file_name);
- length_aligned = (length + 1 + 7) & (~7);
- *extra_length = length_aligned;
+ sample.addr = vma->vm_start;
+ sample.len = vma->vm_end - vma->vm_start;
- return cpu_ctx->mmap_filename;
- }
- }
+ put_mmap_sample(&sample, file_name, length_aligned,
+ vma->vm_pgoff, is_file_exists);
- *extra_length = 0;
- return NULL;
+out:
+ kfree(tmp_buf);
}
-struct quadd_mmap_ctx *quadd_mmap_init(struct quadd_ctx *quadd_ctx)
+int quadd_get_current_mmap(pid_t pid)
{
- int cpu_id;
- u32 *hash;
- char *tmp;
- struct quadd_mmap_cpu_ctx *cpu_ctx;
-
- mmap_ctx.quadd_ctx = quadd_ctx;
-
- hash = kzalloc(QUADD_MMAP_SIZE_ARRAY * sizeof(unsigned int),
- GFP_KERNEL);
- if (!hash) {
- pr_err("Failed to allocate mmap buffer\n");
- return ERR_PTR(-ENOMEM);
+ 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;
}
- mmap_ctx.hash_array = hash;
- mmap_ctx.nr_hashes = 0;
- spin_lock_init(&mmap_ctx.lock);
+ mm = task->mm;
+ if (!mm) {
+ pr_warn("mm is not existed for task: %d\n", pid);
+ return 0;
+ }
- mmap_ctx.cpu_ctx = alloc_percpu(struct quadd_mmap_cpu_ctx);
- if (!mmap_ctx.cpu_ctx)
- return ERR_PTR(-ENOMEM);
+ pr_info("Get mapped memory objects\n");
- for (cpu_id = 0; cpu_id < nr_cpu_ids; cpu_id++) {
- cpu_ctx = per_cpu_ptr(mmap_ctx.cpu_ctx, cpu_id);
+ tmp_buf = kzalloc(PATH_MAX + sizeof(u64), GFP_ATOMIC);
+ if (!tmp_buf)
+ return -ENOMEM;
- tmp = kzalloc(PATH_MAX + sizeof(unsigned long long),
- GFP_KERNEL);
- if (!tmp) {
- pr_err("Failed to allocate mmap buffer\n");
- return ERR_PTR(-ENOMEM);
- }
- cpu_ctx->tmp_buf = tmp;
- }
+ for (vma = mm->mmap; vma; vma = vma->vm_next) {
+ if (!(vma->vm_flags & VM_EXEC))
+ continue;
- return &mmap_ctx;
-}
+ vm_file = vma->vm_file;
+ if (vm_file) {
+ path = &vm_file->f_path;
-void quadd_mmap_reset(void)
-{
- unsigned long flags;
+ file_name = d_path(path, tmp_buf, PATH_MAX);
+ if (IS_ERR(file_name))
+ continue;
- spin_lock_irqsave(&mmap_ctx.lock, flags);
- mmap_ctx.nr_hashes = 0;
- spin_unlock_irqrestore(&mmap_ctx.lock, flags);
-}
+ 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]";
+ }
+ }
-void quadd_mmap_deinit(void)
-{
- int cpu_id;
- unsigned long flags;
- struct quadd_mmap_cpu_ctx *cpu_ctx;
+ if (name)
+ strcpy(tmp_buf, name);
+ else
+ sprintf(tmp_buf, "[vma:%08lx-%08lx]",
+ vma->vm_start, vma->vm_end);
- spin_lock_irqsave(&mmap_ctx.lock, flags);
- kfree(mmap_ctx.hash_array);
- mmap_ctx.hash_array = NULL;
+ file_name = tmp_buf;
+ length = strlen(file_name) + 1;
- for (cpu_id = 0; cpu_id < nr_cpu_ids; cpu_id++) {
- cpu_ctx = per_cpu_ptr(mmap_ctx.cpu_ctx, cpu_id);
- kfree(cpu_ctx->tmp_buf);
+ 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);
}
- free_percpu(mmap_ctx.cpu_ctx);
- spin_unlock_irqrestore(&mmap_ctx.lock, flags);
+ kfree(tmp_buf);
+
+ return 0;
}