task_nommu.c 5.3 KB

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