task_nommu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include <linux/mm.h>
  2. #include <linux/file.h>
  3. #include <linux/fdtable.h>
  4. #include <linux/mount.h>
  5. #include <linux/ptrace.h>
  6. #include <linux/seq_file.h>
  7. #include "internal.h"
  8. /*
  9. * Logic: we've got two memory sums for each process, "shared", and
  10. * "non-shared". Shared memory may get counted more than once, for
  11. * each process that owns it. Non-shared memory is counted
  12. * accurately.
  13. */
  14. void task_mem(struct seq_file *m, struct mm_struct *mm)
  15. {
  16. struct vm_area_struct *vma;
  17. struct vm_region *region;
  18. struct rb_node *p;
  19. unsigned long bytes = 0, sbytes = 0, slack = 0, size;
  20. down_read(&mm->mmap_sem);
  21. for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
  22. vma = rb_entry(p, struct vm_area_struct, vm_rb);
  23. bytes += kobjsize(vma);
  24. region = vma->vm_region;
  25. if (region) {
  26. size = kobjsize(region);
  27. size += region->vm_end - region->vm_start;
  28. } else {
  29. size = vma->vm_end - vma->vm_start;
  30. }
  31. if (atomic_read(&mm->mm_count) > 1 ||
  32. vma->vm_flags & VM_MAYSHARE) {
  33. sbytes += size;
  34. } else {
  35. bytes += size;
  36. if (region)
  37. slack = region->vm_end - vma->vm_end;
  38. }
  39. }
  40. if (atomic_read(&mm->mm_count) > 1)
  41. sbytes += kobjsize(mm);
  42. else
  43. bytes += kobjsize(mm);
  44. if (current->fs && atomic_read(&current->fs->count) > 1)
  45. sbytes += kobjsize(current->fs);
  46. else
  47. bytes += kobjsize(current->fs);
  48. if (current->files && atomic_read(&current->files->count) > 1)
  49. sbytes += kobjsize(current->files);
  50. else
  51. bytes += kobjsize(current->files);
  52. if (current->sighand && atomic_read(&current->sighand->count) > 1)
  53. sbytes += kobjsize(current->sighand);
  54. else
  55. bytes += kobjsize(current->sighand);
  56. bytes += kobjsize(current); /* includes kernel stack */
  57. seq_printf(m,
  58. "Mem:\t%8lu bytes\n"
  59. "Slack:\t%8lu bytes\n"
  60. "Shared:\t%8lu bytes\n",
  61. bytes, slack, sbytes);
  62. up_read(&mm->mmap_sem);
  63. }
  64. unsigned long task_vsize(struct mm_struct *mm)
  65. {
  66. struct vm_area_struct *vma;
  67. struct rb_node *p;
  68. unsigned long vsize = 0;
  69. down_read(&mm->mmap_sem);
  70. for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
  71. vma = rb_entry(p, struct vm_area_struct, vm_rb);
  72. vsize += vma->vm_end - vma->vm_start;
  73. }
  74. up_read(&mm->mmap_sem);
  75. return vsize;
  76. }
  77. int task_statm(struct mm_struct *mm, int *shared, int *text,
  78. int *data, int *resident)
  79. {
  80. struct vm_area_struct *vma;
  81. struct vm_region *region;
  82. struct rb_node *p;
  83. int size = kobjsize(mm);
  84. down_read(&mm->mmap_sem);
  85. for (p = rb_first(&mm->mm_rb); p; p = rb_next(p)) {
  86. vma = rb_entry(p, struct vm_area_struct, vm_rb);
  87. size += kobjsize(vma);
  88. region = vma->vm_region;
  89. if (region) {
  90. size += kobjsize(region);
  91. size += region->vm_end - region->vm_start;
  92. }
  93. }
  94. size += (*text = mm->end_code - mm->start_code);
  95. size += (*data = mm->start_stack - mm->start_data);
  96. up_read(&mm->mmap_sem);
  97. *resident = size;
  98. return size;
  99. }
  100. /*
  101. * display a single VMA to a sequenced file
  102. */
  103. static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
  104. {
  105. unsigned long ino = 0;
  106. struct file *file;
  107. dev_t dev = 0;
  108. int flags, len;
  109. flags = vma->vm_flags;
  110. file = vma->vm_file;
  111. if (file) {
  112. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  113. dev = inode->i_sb->s_dev;
  114. ino = inode->i_ino;
  115. }
  116. seq_printf(m,
  117. "%08lx-%08lx %c%c%c%c %08lx %02x:%02x %lu %n",
  118. vma->vm_start,
  119. vma->vm_end,
  120. flags & VM_READ ? 'r' : '-',
  121. flags & VM_WRITE ? 'w' : '-',
  122. flags & VM_EXEC ? 'x' : '-',
  123. flags & VM_MAYSHARE ? flags & VM_SHARED ? 'S' : 's' : 'p',
  124. vma->vm_pgoff << PAGE_SHIFT,
  125. MAJOR(dev), MINOR(dev), ino, &len);
  126. if (file) {
  127. len = 25 + sizeof(void *) * 6 - len;
  128. if (len < 1)
  129. len = 1;
  130. seq_printf(m, "%*c", len, ' ');
  131. seq_path(m, &file->f_path, "");
  132. }
  133. seq_putc(m, '\n');
  134. return 0;
  135. }
  136. /*
  137. * display mapping lines for a particular process's /proc/pid/maps
  138. */
  139. static int show_map(struct seq_file *m, void *_p)
  140. {
  141. struct rb_node *p = _p;
  142. return nommu_vma_show(m, rb_entry(p, struct vm_area_struct, vm_rb));
  143. }
  144. static void *m_start(struct seq_file *m, loff_t *pos)
  145. {
  146. struct proc_maps_private *priv = m->private;
  147. struct mm_struct *mm;
  148. struct rb_node *p;
  149. loff_t n = *pos;
  150. /* pin the task and mm whilst we play with them */
  151. priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
  152. if (!priv->task)
  153. return NULL;
  154. mm = mm_for_maps(priv->task);
  155. if (!mm) {
  156. put_task_struct(priv->task);
  157. priv->task = NULL;
  158. return NULL;
  159. }
  160. /* start from the Nth VMA */
  161. for (p = rb_first(&mm->mm_rb); p; p = rb_next(p))
  162. if (n-- == 0)
  163. return p;
  164. return NULL;
  165. }
  166. static void m_stop(struct seq_file *m, void *_vml)
  167. {
  168. struct proc_maps_private *priv = m->private;
  169. if (priv->task) {
  170. struct mm_struct *mm = priv->task->mm;
  171. up_read(&mm->mmap_sem);
  172. mmput(mm);
  173. put_task_struct(priv->task);
  174. }
  175. }
  176. static void *m_next(struct seq_file *m, void *_p, loff_t *pos)
  177. {
  178. struct rb_node *p = _p;
  179. (*pos)++;
  180. return p ? rb_next(p) : NULL;
  181. }
  182. static const struct seq_operations proc_pid_maps_ops = {
  183. .start = m_start,
  184. .next = m_next,
  185. .stop = m_stop,
  186. .show = show_map
  187. };
  188. static int maps_open(struct inode *inode, struct file *file)
  189. {
  190. struct proc_maps_private *priv;
  191. int ret = -ENOMEM;
  192. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  193. if (priv) {
  194. priv->pid = proc_pid(inode);
  195. ret = seq_open(file, &proc_pid_maps_ops);
  196. if (!ret) {
  197. struct seq_file *m = file->private_data;
  198. m->private = priv;
  199. } else {
  200. kfree(priv);
  201. }
  202. }
  203. return ret;
  204. }
  205. const struct file_operations proc_maps_operations = {
  206. .open = maps_open,
  207. .read = seq_read,
  208. .llseek = seq_lseek,
  209. .release = seq_release_private,
  210. };