task_nommu.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 pid_t pid_of_stack(struct proc_maps_private *priv,
  108. struct vm_area_struct *vma, bool is_pid)
  109. {
  110. struct inode *inode = priv->inode;
  111. struct task_struct *task;
  112. pid_t ret = 0;
  113. rcu_read_lock();
  114. task = pid_task(proc_pid(inode), PIDTYPE_PID);
  115. if (task) {
  116. task = task_of_stack(task, vma, is_pid);
  117. if (task)
  118. ret = task_pid_nr_ns(task, inode->i_sb->s_fs_info);
  119. }
  120. rcu_read_unlock();
  121. return ret;
  122. }
  123. /*
  124. * display a single VMA to a sequenced file
  125. */
  126. static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
  127. int is_pid)
  128. {
  129. struct mm_struct *mm = vma->vm_mm;
  130. struct proc_maps_private *priv = m->private;
  131. unsigned long ino = 0;
  132. struct file *file;
  133. dev_t dev = 0;
  134. int flags;
  135. unsigned long long pgoff = 0;
  136. flags = vma->vm_flags;
  137. file = vma->vm_file;
  138. if (file) {
  139. struct inode *inode = file_inode(vma->vm_file);
  140. dev = inode->i_sb->s_dev;
  141. ino = inode->i_ino;
  142. pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  143. }
  144. seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
  145. seq_printf(m,
  146. "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
  147. vma->vm_start,
  148. vma->vm_end,
  149. flags & VM_READ ? 'r' : '-',
  150. flags & VM_WRITE ? 'w' : '-',
  151. flags & VM_EXEC ? 'x' : '-',
  152. flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p',
  153. pgoff,
  154. MAJOR(dev), MINOR(dev), ino);
  155. if (file) {
  156. seq_pad(m, ' ');
  157. seq_file_path(m, file, "");
  158. } else if (mm) {
  159. pid_t tid = pid_of_stack(priv, vma, is_pid);
  160. if (tid != 0) {
  161. seq_pad(m, ' ');
  162. /*
  163. * Thread stack in /proc/PID/task/TID/maps or
  164. * the main process stack.
  165. */
  166. if (!is_pid || (vma->vm_start <= mm->start_stack &&
  167. vma->vm_end >= mm->start_stack))
  168. seq_printf(m, "[stack]");
  169. else
  170. seq_printf(m, "[stack:%d]", tid);
  171. }
  172. }
  173. seq_putc(m, '\n');
  174. return 0;
  175. }
  176. /*
  177. * display mapping lines for a particular process's /proc/pid/maps
  178. */
  179. static int show_map(struct seq_file *m, void *_p, int is_pid)
  180. {
  181. struct rb_node *p = _p;
  182. return nommu_vma_show(m, rb_entry(p, struct vm_area_struct, vm_rb),
  183. is_pid);
  184. }
  185. static int show_pid_map(struct seq_file *m, void *_p)
  186. {
  187. return show_map(m, _p, 1);
  188. }
  189. static int show_tid_map(struct seq_file *m, void *_p)
  190. {
  191. return show_map(m, _p, 0);
  192. }
  193. static void *m_start(struct seq_file *m, loff_t *pos)
  194. {
  195. struct proc_maps_private *priv = m->private;
  196. struct mm_struct *mm;
  197. struct rb_node *p;
  198. loff_t n = *pos;
  199. /* pin the task and mm whilst we play with them */
  200. priv->task = get_proc_task(priv->inode);
  201. if (!priv->task)
  202. return ERR_PTR(-ESRCH);
  203. mm = priv->mm;
  204. if (!mm || !atomic_inc_not_zero(&mm->mm_users))
  205. return NULL;
  206. down_read(&mm->mmap_sem);
  207. /* start from the Nth VMA */
  208. for (p = rb_first(&mm->mm_rb); p; p = rb_next(p))
  209. if (n-- == 0)
  210. return p;
  211. up_read(&mm->mmap_sem);
  212. mmput(mm);
  213. return NULL;
  214. }
  215. static void m_stop(struct seq_file *m, void *_vml)
  216. {
  217. struct proc_maps_private *priv = m->private;
  218. if (!IS_ERR_OR_NULL(_vml)) {
  219. up_read(&priv->mm->mmap_sem);
  220. mmput(priv->mm);
  221. }
  222. if (priv->task) {
  223. put_task_struct(priv->task);
  224. priv->task = NULL;
  225. }
  226. }
  227. static void *m_next(struct seq_file *m, void *_p, loff_t *pos)
  228. {
  229. struct rb_node *p = _p;
  230. (*pos)++;
  231. return p ? rb_next(p) : NULL;
  232. }
  233. static const struct seq_operations proc_pid_maps_ops = {
  234. .start = m_start,
  235. .next = m_next,
  236. .stop = m_stop,
  237. .show = show_pid_map
  238. };
  239. static const struct seq_operations proc_tid_maps_ops = {
  240. .start = m_start,
  241. .next = m_next,
  242. .stop = m_stop,
  243. .show = show_tid_map
  244. };
  245. static int maps_open(struct inode *inode, struct file *file,
  246. const struct seq_operations *ops)
  247. {
  248. struct proc_maps_private *priv;
  249. priv = __seq_open_private(file, ops, sizeof(*priv));
  250. if (!priv)
  251. return -ENOMEM;
  252. priv->inode = inode;
  253. priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
  254. if (IS_ERR(priv->mm)) {
  255. int err = PTR_ERR(priv->mm);
  256. seq_release_private(inode, file);
  257. return err;
  258. }
  259. return 0;
  260. }
  261. static int map_release(struct inode *inode, struct file *file)
  262. {
  263. struct seq_file *seq = file->private_data;
  264. struct proc_maps_private *priv = seq->private;
  265. if (priv->mm)
  266. mmdrop(priv->mm);
  267. return seq_release_private(inode, file);
  268. }
  269. static int pid_maps_open(struct inode *inode, struct file *file)
  270. {
  271. return maps_open(inode, file, &proc_pid_maps_ops);
  272. }
  273. static int tid_maps_open(struct inode *inode, struct file *file)
  274. {
  275. return maps_open(inode, file, &proc_tid_maps_ops);
  276. }
  277. const struct file_operations proc_pid_maps_operations = {
  278. .open = pid_maps_open,
  279. .read = seq_read,
  280. .llseek = seq_lseek,
  281. .release = map_release,
  282. };
  283. const struct file_operations proc_tid_maps_operations = {
  284. .open = tid_maps_open,
  285. .read = seq_read,
  286. .llseek = seq_lseek,
  287. .release = map_release,
  288. };