task_nommu.c 6.6 KB

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