task_nommu.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 then 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_list_struct *vml;
  17. unsigned long bytes = 0, sbytes = 0, slack = 0;
  18. down_read(&mm->mmap_sem);
  19. for (vml = mm->context.vmlist; vml; vml = vml->next) {
  20. if (!vml->vma)
  21. continue;
  22. bytes += kobjsize(vml);
  23. if (atomic_read(&mm->mm_count) > 1 ||
  24. atomic_read(&vml->vma->vm_usage) > 1
  25. ) {
  26. sbytes += kobjsize((void *) vml->vma->vm_start);
  27. sbytes += kobjsize(vml->vma);
  28. } else {
  29. bytes += kobjsize((void *) vml->vma->vm_start);
  30. bytes += kobjsize(vml->vma);
  31. slack += kobjsize((void *) vml->vma->vm_start) -
  32. (vml->vma->vm_end - vml->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_list_struct *tbp;
  62. unsigned long vsize = 0;
  63. down_read(&mm->mmap_sem);
  64. for (tbp = mm->context.vmlist; tbp; tbp = tbp->next) {
  65. if (tbp->vma)
  66. vsize += kobjsize((void *) tbp->vma->vm_start);
  67. }
  68. up_read(&mm->mmap_sem);
  69. return vsize;
  70. }
  71. int task_statm(struct mm_struct *mm, int *shared, int *text,
  72. int *data, int *resident)
  73. {
  74. struct vm_list_struct *tbp;
  75. int size = kobjsize(mm);
  76. down_read(&mm->mmap_sem);
  77. for (tbp = mm->context.vmlist; tbp; tbp = tbp->next) {
  78. size += kobjsize(tbp);
  79. if (tbp->vma) {
  80. size += kobjsize(tbp->vma);
  81. size += kobjsize((void *) tbp->vma->vm_start);
  82. }
  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 mapping lines for a particular process's /proc/pid/maps
  92. */
  93. static int show_map(struct seq_file *m, void *_vml)
  94. {
  95. struct vm_list_struct *vml = _vml;
  96. return nommu_vma_show(m, vml->vma);
  97. }
  98. static void *m_start(struct seq_file *m, loff_t *pos)
  99. {
  100. struct proc_maps_private *priv = m->private;
  101. struct vm_list_struct *vml;
  102. struct mm_struct *mm;
  103. loff_t n = *pos;
  104. /* pin the task and mm whilst we play with them */
  105. priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
  106. if (!priv->task)
  107. return NULL;
  108. mm = mm_for_maps(priv->task);
  109. if (!mm) {
  110. put_task_struct(priv->task);
  111. priv->task = NULL;
  112. return NULL;
  113. }
  114. /* start from the Nth VMA */
  115. for (vml = mm->context.vmlist; vml; vml = vml->next)
  116. if (n-- == 0)
  117. return vml;
  118. return NULL;
  119. }
  120. static void m_stop(struct seq_file *m, void *_vml)
  121. {
  122. struct proc_maps_private *priv = m->private;
  123. if (priv->task) {
  124. struct mm_struct *mm = priv->task->mm;
  125. up_read(&mm->mmap_sem);
  126. mmput(mm);
  127. put_task_struct(priv->task);
  128. }
  129. }
  130. static void *m_next(struct seq_file *m, void *_vml, loff_t *pos)
  131. {
  132. struct vm_list_struct *vml = _vml;
  133. (*pos)++;
  134. return vml ? vml->next : NULL;
  135. }
  136. static const struct seq_operations proc_pid_maps_ops = {
  137. .start = m_start,
  138. .next = m_next,
  139. .stop = m_stop,
  140. .show = show_map
  141. };
  142. static int maps_open(struct inode *inode, struct file *file)
  143. {
  144. struct proc_maps_private *priv;
  145. int ret = -ENOMEM;
  146. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  147. if (priv) {
  148. priv->pid = proc_pid(inode);
  149. ret = seq_open(file, &proc_pid_maps_ops);
  150. if (!ret) {
  151. struct seq_file *m = file->private_data;
  152. m->private = priv;
  153. } else {
  154. kfree(priv);
  155. }
  156. }
  157. return ret;
  158. }
  159. const struct file_operations proc_maps_operations = {
  160. .open = maps_open,
  161. .read = seq_read,
  162. .llseek = seq_lseek,
  163. .release = seq_release_private,
  164. };