task_nommu.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include <linux/mm.h>
  2. #include <linux/file.h>
  3. #include <linux/fdtable.h>
  4. #include <linux/fs_struct.h>
  5. #include <linux/mount.h>
  6. #include <linux/ptrace.h>
  7. #include <linux/slab.h>
  8. #include <linux/seq_file.h>
  9. #include "internal.h"
  10. /*
  11. * Logic: we've got two memory sums for each process, "shared", and
  12. * "non-shared". Shared memory may get counted more than once, for
  13. * each process that owns it. Non-shared memory is counted
  14. * accurately.
  15. */
  16. void task_mem(struct seq_file *m, struct mm_struct *mm)
  17. {
  18. struct vm_area_struct *vma;
  19. struct vm_region *region;
  20. struct rb_node *p;
  21. unsigned long bytes = 0, sbytes = 0, slack = 0, size;
  22. down_read(&mm->mmap_sem);
  23. for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
  24. vma = rb_entry(p, struct vm_area_struct, vm_rb);
  25. bytes += kobjsize(vma);
  26. region = vma->vm_region;
  27. if (region) {
  28. size = kobjsize(region);
  29. size += region->vm_end - region->vm_start;
  30. } else {
  31. size = vma->vm_end - vma->vm_start;
  32. }
  33. if (atomic_read(&mm->mm_count) > 1 ||
  34. vma->vm_flags & VM_MAYSHARE) {
  35. sbytes += size;
  36. } else {
  37. bytes += size;
  38. if (region)
  39. slack = region->vm_end - vma->vm_end;
  40. }
  41. }
  42. if (atomic_read(&mm->mm_count) > 1)
  43. sbytes += kobjsize(mm);
  44. else
  45. bytes += kobjsize(mm);
  46. if (current->fs && current->fs->users > 1)
  47. sbytes += kobjsize(current->fs);
  48. else
  49. bytes += kobjsize(current->fs);
  50. if (current->files && atomic_read(&current->files->count) > 1)
  51. sbytes += kobjsize(current->files);
  52. else
  53. bytes += kobjsize(current->files);
  54. if (current->sighand && atomic_read(&current->sighand->count) > 1)
  55. sbytes += kobjsize(current->sighand);
  56. else
  57. bytes += kobjsize(current->sighand);
  58. bytes += kobjsize(current); /* includes kernel stack */
  59. seq_printf(m,
  60. "Mem:\t%8lu bytes\n"
  61. "Slack:\t%8lu bytes\n"
  62. "Shared:\t%8lu bytes\n",
  63. bytes, slack, sbytes);
  64. up_read(&mm->mmap_sem);
  65. }
  66. unsigned long task_vsize(struct mm_struct *mm)
  67. {
  68. struct vm_area_struct *vma;
  69. struct rb_node *p;
  70. unsigned long vsize = 0;
  71. down_read(&mm->mmap_sem);
  72. for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
  73. vma = rb_entry(p, struct vm_area_struct, vm_rb);
  74. vsize += vma->vm_end - vma->vm_start;
  75. }
  76. up_read(&mm->mmap_sem);
  77. return vsize;
  78. }
  79. unsigned long task_statm(struct mm_struct *mm,
  80. unsigned long *shared, unsigned long *text,
  81. unsigned long *data, unsigned long *resident)
  82. {
  83. struct vm_area_struct *vma;
  84. struct vm_region *region;
  85. struct rb_node *p;
  86. unsigned long size = kobjsize(mm);
  87. down_read(&mm->mmap_sem);
  88. for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
  89. vma = rb_entry(p, struct vm_area_struct, vm_rb);
  90. size += kobjsize(vma);
  91. region = vma->vm_region;
  92. if (region) {
  93. size += kobjsize(region);
  94. size += region->vm_end - region->vm_start;
  95. }
  96. }
  97. *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
  98. >> PAGE_SHIFT;
  99. *data = (PAGE_ALIGN(mm->start_stack) - (mm->start_data & PAGE_MASK))
  100. >> PAGE_SHIFT;
  101. up_read(&mm->mmap_sem);
  102. size >>= PAGE_SHIFT;
  103. size += *text + *data;
  104. *resident = size;
  105. return size;
  106. }
  107. static int is_stack(struct proc_maps_private *priv,
  108. struct vm_area_struct *vma)
  109. {
  110. struct mm_struct *mm = vma->vm_mm;
  111. /*
  112. * We make no effort to guess what a given thread considers to be
  113. * its "stack". It's not even well-defined for programs written
  114. * languages like Go.
  115. */
  116. return vma->vm_start <= mm->start_stack &&
  117. vma->vm_end >= mm->start_stack;
  118. }
  119. /*
  120. * display a single VMA to a sequenced file
  121. */
  122. static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
  123. int is_pid)
  124. {
  125. struct mm_struct *mm = vma->vm_mm;
  126. struct proc_maps_private *priv = m->private;
  127. unsigned long ino = 0;
  128. struct file *file;
  129. dev_t dev = 0;
  130. int flags;
  131. unsigned long long pgoff = 0;
  132. flags = vma->vm_flags;
  133. file = vma->vm_file;
  134. if (file) {
  135. struct inode *inode = file_inode(vma->vm_file);
  136. dev = inode->i_sb->s_dev;
  137. ino = inode->i_ino;
  138. pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  139. }
  140. seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
  141. seq_printf(m,
  142. "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
  143. vma->vm_start,
  144. vma->vm_end,
  145. flags & VM_READ ? 'r' : '-',
  146. flags & VM_WRITE ? 'w' : '-',
  147. flags & VM_EXEC ? 'x' : '-',
  148. flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p',
  149. pgoff,
  150. MAJOR(dev), MINOR(dev), ino);
  151. if (file) {
  152. seq_pad(m, ' ');
  153. seq_file_path(m, file, "");
  154. } else if (mm && is_stack(priv, vma)) {
  155. seq_pad(m, ' ');
  156. seq_printf(m, "[stack]");
  157. }
  158. seq_putc(m, '\n');
  159. return 0;
  160. }
  161. /*
  162. * display mapping lines for a particular process's /proc/pid/maps
  163. */
  164. static int show_map(struct seq_file *m, void *_p, int is_pid)
  165. {
  166. struct rb_node *p = _p;
  167. return nommu_vma_show(m, rb_entry(p, struct vm_area_struct, vm_rb),
  168. is_pid);
  169. }
  170. static int show_pid_map(struct seq_file *m, void *_p)
  171. {
  172. return show_map(m, _p, 1);
  173. }
  174. static int show_tid_map(struct seq_file *m, void *_p)
  175. {
  176. return show_map(m, _p, 0);
  177. }
  178. static void *m_start(struct seq_file *m, loff_t *pos)
  179. {
  180. struct proc_maps_private *priv = m->private;
  181. struct mm_struct *mm;
  182. struct rb_node *p;
  183. loff_t n = *pos;
  184. /* pin the task and mm whilst we play with them */
  185. priv->task = get_proc_task(priv->inode);
  186. if (!priv->task)
  187. return ERR_PTR(-ESRCH);
  188. mm = priv->mm;
  189. if (!mm || !atomic_inc_not_zero(&mm->mm_users))
  190. return NULL;
  191. down_read(&mm->mmap_sem);
  192. /* start from the Nth VMA */
  193. for (p = rb_first(&mm->mm_rb); p; p = rb_next(p))
  194. if (n-- == 0)
  195. return p;
  196. up_read(&mm->mmap_sem);
  197. mmput(mm);
  198. return NULL;
  199. }
  200. static void m_stop(struct seq_file *m, void *_vml)
  201. {
  202. struct proc_maps_private *priv = m->private;
  203. if (!IS_ERR_OR_NULL(_vml)) {
  204. up_read(&priv->mm->mmap_sem);
  205. mmput(priv->mm);
  206. }
  207. if (priv->task) {
  208. put_task_struct(priv->task);
  209. priv->task = NULL;
  210. }
  211. }
  212. static void *m_next(struct seq_file *m, void *_p, loff_t *pos)
  213. {
  214. struct rb_node *p = _p;
  215. (*pos)++;
  216. return p ? rb_next(p) : NULL;
  217. }
  218. static const struct seq_operations proc_pid_maps_ops = {
  219. .start = m_start,
  220. .next = m_next,
  221. .stop = m_stop,
  222. .show = show_pid_map
  223. };
  224. static const struct seq_operations proc_tid_maps_ops = {
  225. .start = m_start,
  226. .next = m_next,
  227. .stop = m_stop,
  228. .show = show_tid_map
  229. };
  230. static int maps_open(struct inode *inode, struct file *file,
  231. const struct seq_operations *ops)
  232. {
  233. struct proc_maps_private *priv;
  234. priv = __seq_open_private(file, ops, sizeof(*priv));
  235. if (!priv)
  236. return -ENOMEM;
  237. priv->inode = inode;
  238. priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
  239. if (IS_ERR(priv->mm)) {
  240. int err = PTR_ERR(priv->mm);
  241. seq_release_private(inode, file);
  242. return err;
  243. }
  244. return 0;
  245. }
  246. static int map_release(struct inode *inode, struct file *file)
  247. {
  248. struct seq_file *seq = file->private_data;
  249. struct proc_maps_private *priv = seq->private;
  250. if (priv->mm)
  251. mmdrop(priv->mm);
  252. return seq_release_private(inode, file);
  253. }
  254. static int pid_maps_open(struct inode *inode, struct file *file)
  255. {
  256. return maps_open(inode, file, &proc_pid_maps_ops);
  257. }
  258. static int tid_maps_open(struct inode *inode, struct file *file)
  259. {
  260. return maps_open(inode, file, &proc_tid_maps_ops);
  261. }
  262. const struct file_operations proc_pid_maps_operations = {
  263. .open = pid_maps_open,
  264. .read = seq_read,
  265. .llseek = seq_lseek,
  266. .release = map_release,
  267. };
  268. const struct file_operations proc_tid_maps_operations = {
  269. .open = tid_maps_open,
  270. .read = seq_read,
  271. .llseek = seq_lseek,
  272. .release = map_release,
  273. };