task_nommu.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. is_nommu_shared_mapping(vma->vm_flags)) {
  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. mmap_read_unlock(mm);
  61. seq_printf(m,
  62. "Mem:\t%8lu bytes\n"
  63. "Slack:\t%8lu bytes\n"
  64. "Shared:\t%8lu bytes\n",
  65. bytes, slack, sbytes);
  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. /*
  106. * display a single VMA to a sequenced file
  107. */
  108. static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
  109. {
  110. struct mm_struct *mm = vma->vm_mm;
  111. unsigned long ino = 0;
  112. struct file *file;
  113. dev_t dev = 0;
  114. int flags;
  115. unsigned long long pgoff = 0;
  116. flags = vma->vm_flags;
  117. file = vma->vm_file;
  118. if (file) {
  119. struct inode *inode = file_inode(vma->vm_file);
  120. dev = inode->i_sb->s_dev;
  121. ino = inode->i_ino;
  122. pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  123. }
  124. seq_setwidth(m, 25 + sizeof(void *) * 6 - 1);
  125. seq_printf(m,
  126. "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu ",
  127. vma->vm_start,
  128. vma->vm_end,
  129. flags & VM_READ ? 'r' : '-',
  130. flags & VM_WRITE ? 'w' : '-',
  131. flags & VM_EXEC ? 'x' : '-',
  132. flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p',
  133. pgoff,
  134. MAJOR(dev), MINOR(dev), ino);
  135. if (file) {
  136. seq_pad(m, ' ');
  137. seq_path(m, file_user_path(file), "");
  138. } else if (mm && vma_is_initial_stack(vma)) {
  139. seq_pad(m, ' ');
  140. seq_puts(m, "[stack]");
  141. }
  142. seq_putc(m, '\n');
  143. return 0;
  144. }
  145. /*
  146. * display mapping lines for a particular process's /proc/pid/maps
  147. */
  148. static int show_map(struct seq_file *m, void *_p)
  149. {
  150. return nommu_vma_show(m, _p);
  151. }
  152. static struct vm_area_struct *proc_get_vma(struct proc_maps_private *priv,
  153. loff_t *ppos)
  154. {
  155. struct vm_area_struct *vma = vma_next(&priv->iter);
  156. if (vma) {
  157. *ppos = vma->vm_start;
  158. } else {
  159. *ppos = -1UL;
  160. }
  161. return vma;
  162. }
  163. static void *m_start(struct seq_file *m, loff_t *ppos)
  164. {
  165. struct proc_maps_private *priv = m->private;
  166. unsigned long last_addr = *ppos;
  167. struct mm_struct *mm;
  168. /* See proc_get_vma(). Zero at the start or after lseek. */
  169. if (last_addr == -1UL)
  170. return NULL;
  171. /* pin the task and mm whilst we play with them */
  172. priv->task = get_proc_task(priv->inode);
  173. if (!priv->task)
  174. return ERR_PTR(-ESRCH);
  175. mm = priv->mm;
  176. if (!mm || !mmget_not_zero(mm)) {
  177. put_task_struct(priv->task);
  178. priv->task = NULL;
  179. return NULL;
  180. }
  181. if (mmap_read_lock_killable(mm)) {
  182. mmput(mm);
  183. put_task_struct(priv->task);
  184. priv->task = NULL;
  185. return ERR_PTR(-EINTR);
  186. }
  187. vma_iter_init(&priv->iter, mm, last_addr);
  188. return proc_get_vma(priv, ppos);
  189. }
  190. static void m_stop(struct seq_file *m, void *v)
  191. {
  192. struct proc_maps_private *priv = m->private;
  193. struct mm_struct *mm = priv->mm;
  194. if (!priv->task)
  195. return;
  196. mmap_read_unlock(mm);
  197. mmput(mm);
  198. put_task_struct(priv->task);
  199. priv->task = NULL;
  200. }
  201. static void *m_next(struct seq_file *m, void *_p, loff_t *ppos)
  202. {
  203. return proc_get_vma(m->private, ppos);
  204. }
  205. static const struct seq_operations proc_pid_maps_ops = {
  206. .start = m_start,
  207. .next = m_next,
  208. .stop = m_stop,
  209. .show = show_map
  210. };
  211. static int maps_open(struct inode *inode, struct file *file,
  212. const struct seq_operations *ops)
  213. {
  214. struct proc_maps_private *priv;
  215. priv = __seq_open_private(file, ops, sizeof(*priv));
  216. if (!priv)
  217. return -ENOMEM;
  218. priv->inode = inode;
  219. priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
  220. if (IS_ERR(priv->mm)) {
  221. int err = PTR_ERR(priv->mm);
  222. seq_release_private(inode, file);
  223. return err;
  224. }
  225. return 0;
  226. }
  227. static int map_release(struct inode *inode, struct file *file)
  228. {
  229. struct seq_file *seq = file->private_data;
  230. struct proc_maps_private *priv = seq->private;
  231. if (priv->mm)
  232. mmdrop(priv->mm);
  233. return seq_release_private(inode, file);
  234. }
  235. static int pid_maps_open(struct inode *inode, struct file *file)
  236. {
  237. return maps_open(inode, file, &proc_pid_maps_ops);
  238. }
  239. const struct file_operations proc_pid_maps_operations = {
  240. .open = pid_maps_open,
  241. .read = seq_read,
  242. .llseek = seq_lseek,
  243. .release = map_release,
  244. };