task_mmu.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <linux/mm.h>
  2. #include <linux/hugetlb.h>
  3. #include <linux/mount.h>
  4. #include <linux/seq_file.h>
  5. #include <asm/elf.h>
  6. #include <asm/uaccess.h>
  7. #include "internal.h"
  8. char *task_mem(struct mm_struct *mm, char *buffer)
  9. {
  10. unsigned long data, text, lib;
  11. data = mm->total_vm - mm->shared_vm - mm->stack_vm;
  12. text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
  13. lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
  14. buffer += sprintf(buffer,
  15. "VmSize:\t%8lu kB\n"
  16. "VmLck:\t%8lu kB\n"
  17. "VmRSS:\t%8lu kB\n"
  18. "VmData:\t%8lu kB\n"
  19. "VmStk:\t%8lu kB\n"
  20. "VmExe:\t%8lu kB\n"
  21. "VmLib:\t%8lu kB\n"
  22. "VmPTE:\t%8lu kB\n",
  23. (mm->total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
  24. mm->locked_vm << (PAGE_SHIFT-10),
  25. get_mm_counter(mm, rss) << (PAGE_SHIFT-10),
  26. data << (PAGE_SHIFT-10),
  27. mm->stack_vm << (PAGE_SHIFT-10), text, lib,
  28. (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10);
  29. return buffer;
  30. }
  31. unsigned long task_vsize(struct mm_struct *mm)
  32. {
  33. return PAGE_SIZE * mm->total_vm;
  34. }
  35. int task_statm(struct mm_struct *mm, int *shared, int *text,
  36. int *data, int *resident)
  37. {
  38. int rss = get_mm_counter(mm, rss);
  39. *shared = rss - get_mm_counter(mm, anon_rss);
  40. *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
  41. >> PAGE_SHIFT;
  42. *data = mm->total_vm - mm->shared_vm;
  43. *resident = rss;
  44. return mm->total_vm;
  45. }
  46. int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  47. {
  48. struct vm_area_struct * vma;
  49. int result = -ENOENT;
  50. struct task_struct *task = proc_task(inode);
  51. struct mm_struct * mm = get_task_mm(task);
  52. if (!mm)
  53. goto out;
  54. down_read(&mm->mmap_sem);
  55. vma = mm->mmap;
  56. while (vma) {
  57. if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
  58. break;
  59. vma = vma->vm_next;
  60. }
  61. if (vma) {
  62. *mnt = mntget(vma->vm_file->f_vfsmnt);
  63. *dentry = dget(vma->vm_file->f_dentry);
  64. result = 0;
  65. }
  66. up_read(&mm->mmap_sem);
  67. mmput(mm);
  68. out:
  69. return result;
  70. }
  71. static void pad_len_spaces(struct seq_file *m, int len)
  72. {
  73. len = 25 + sizeof(void*) * 6 - len;
  74. if (len < 1)
  75. len = 1;
  76. seq_printf(m, "%*c", len, ' ');
  77. }
  78. static int show_map(struct seq_file *m, void *v)
  79. {
  80. struct task_struct *task = m->private;
  81. struct vm_area_struct *map = v;
  82. struct mm_struct *mm = map->vm_mm;
  83. struct file *file = map->vm_file;
  84. int flags = map->vm_flags;
  85. unsigned long ino = 0;
  86. dev_t dev = 0;
  87. int len;
  88. if (file) {
  89. struct inode *inode = map->vm_file->f_dentry->d_inode;
  90. dev = inode->i_sb->s_dev;
  91. ino = inode->i_ino;
  92. }
  93. seq_printf(m, "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
  94. map->vm_start,
  95. map->vm_end,
  96. flags & VM_READ ? 'r' : '-',
  97. flags & VM_WRITE ? 'w' : '-',
  98. flags & VM_EXEC ? 'x' : '-',
  99. flags & VM_MAYSHARE ? 's' : 'p',
  100. map->vm_pgoff << PAGE_SHIFT,
  101. MAJOR(dev), MINOR(dev), ino, &len);
  102. /*
  103. * Print the dentry name for named mappings, and a
  104. * special [heap] marker for the heap:
  105. */
  106. if (map->vm_file) {
  107. pad_len_spaces(m, len);
  108. seq_path(m, file->f_vfsmnt, file->f_dentry, "");
  109. } else {
  110. if (mm) {
  111. if (map->vm_start <= mm->start_brk &&
  112. map->vm_end >= mm->brk) {
  113. pad_len_spaces(m, len);
  114. seq_puts(m, "[heap]");
  115. } else {
  116. if (map->vm_start <= mm->start_stack &&
  117. map->vm_end >= mm->start_stack) {
  118. pad_len_spaces(m, len);
  119. seq_puts(m, "[stack]");
  120. }
  121. }
  122. } else {
  123. pad_len_spaces(m, len);
  124. seq_puts(m, "[vdso]");
  125. }
  126. }
  127. seq_putc(m, '\n');
  128. if (m->count < m->size) /* map is copied successfully */
  129. m->version = (map != get_gate_vma(task))? map->vm_start: 0;
  130. return 0;
  131. }
  132. static void *m_start(struct seq_file *m, loff_t *pos)
  133. {
  134. struct task_struct *task = m->private;
  135. unsigned long last_addr = m->version;
  136. struct mm_struct *mm;
  137. struct vm_area_struct *map, *tail_map;
  138. loff_t l = *pos;
  139. /*
  140. * We remember last_addr rather than next_addr to hit with
  141. * mmap_cache most of the time. We have zero last_addr at
  142. * the begining and also after lseek. We will have -1 last_addr
  143. * after the end of the maps.
  144. */
  145. if (last_addr == -1UL)
  146. return NULL;
  147. mm = get_task_mm(task);
  148. if (!mm)
  149. return NULL;
  150. tail_map = get_gate_vma(task);
  151. down_read(&mm->mmap_sem);
  152. /* Start with last addr hint */
  153. if (last_addr && (map = find_vma(mm, last_addr))) {
  154. map = map->vm_next;
  155. goto out;
  156. }
  157. /*
  158. * Check the map index is within the range and do
  159. * sequential scan until m_index.
  160. */
  161. map = NULL;
  162. if ((unsigned long)l < mm->map_count) {
  163. map = mm->mmap;
  164. while (l-- && map)
  165. map = map->vm_next;
  166. goto out;
  167. }
  168. if (l != mm->map_count)
  169. tail_map = NULL; /* After gate map */
  170. out:
  171. if (map)
  172. return map;
  173. /* End of maps has reached */
  174. m->version = (tail_map != NULL)? 0: -1UL;
  175. up_read(&mm->mmap_sem);
  176. mmput(mm);
  177. return tail_map;
  178. }
  179. static void m_stop(struct seq_file *m, void *v)
  180. {
  181. struct task_struct *task = m->private;
  182. struct vm_area_struct *map = v;
  183. if (map && map != get_gate_vma(task)) {
  184. struct mm_struct *mm = map->vm_mm;
  185. up_read(&mm->mmap_sem);
  186. mmput(mm);
  187. }
  188. }
  189. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  190. {
  191. struct task_struct *task = m->private;
  192. struct vm_area_struct *map = v;
  193. struct vm_area_struct *tail_map = get_gate_vma(task);
  194. (*pos)++;
  195. if (map && (map != tail_map) && map->vm_next)
  196. return map->vm_next;
  197. m_stop(m, v);
  198. return (map != tail_map)? tail_map: NULL;
  199. }
  200. struct seq_operations proc_pid_maps_op = {
  201. .start = m_start,
  202. .next = m_next,
  203. .stop = m_stop,
  204. .show = show_map
  205. };