mm: proc: smaps_rollup: do not stall write attempts on mmap_lock
[linux-2.6-microblaze.git] / fs / proc / task_mmu.c
index f777275..846d43d 100644 (file)
@@ -865,9 +865,73 @@ static int show_smaps_rollup(struct seq_file *m, void *v)
 
        hold_task_mempolicy(priv);
 
-       for (vma = priv->mm->mmap; vma; vma = vma->vm_next) {
+       for (vma = priv->mm->mmap; vma;) {
                smap_gather_stats(vma, &mss, 0);
                last_vma_end = vma->vm_end;
+
+               /*
+                * Release mmap_lock temporarily if someone wants to
+                * access it for write request.
+                */
+               if (mmap_lock_is_contended(mm)) {
+                       mmap_read_unlock(mm);
+                       ret = mmap_read_lock_killable(mm);
+                       if (ret) {
+                               release_task_mempolicy(priv);
+                               goto out_put_mm;
+                       }
+
+                       /*
+                        * After dropping the lock, there are four cases to
+                        * consider. See the following example for explanation.
+                        *
+                        *   +------+------+-----------+
+                        *   | VMA1 | VMA2 | VMA3      |
+                        *   +------+------+-----------+
+                        *   |      |      |           |
+                        *  4k     8k     16k         400k
+                        *
+                        * Suppose we drop the lock after reading VMA2 due to
+                        * contention, then we get:
+                        *
+                        *      last_vma_end = 16k
+                        *
+                        * 1) VMA2 is freed, but VMA3 exists:
+                        *
+                        *    find_vma(mm, 16k - 1) will return VMA3.
+                        *    In this case, just continue from VMA3.
+                        *
+                        * 2) VMA2 still exists:
+                        *
+                        *    find_vma(mm, 16k - 1) will return VMA2.
+                        *    Iterate the loop like the original one.
+                        *
+                        * 3) No more VMAs can be found:
+                        *
+                        *    find_vma(mm, 16k - 1) will return NULL.
+                        *    No more things to do, just break.
+                        *
+                        * 4) (last_vma_end - 1) is the middle of a vma (VMA'):
+                        *
+                        *    find_vma(mm, 16k - 1) will return VMA' whose range
+                        *    contains last_vma_end.
+                        *    Iterate VMA' from last_vma_end.
+                        */
+                       vma = find_vma(mm, last_vma_end - 1);
+                       /* Case 3 above */
+                       if (!vma)
+                               break;
+
+                       /* Case 1 above */
+                       if (vma->vm_start >= last_vma_end)
+                               continue;
+
+                       /* Case 4 above */
+                       if (vma->vm_end > last_vma_end)
+                               smap_gather_stats(vma, &mss, last_vma_end);
+               }
+               /* Case 2 above */
+               vma = vma->vm_next;
        }
 
        show_vma_header_prefix(m, priv->mm->mmap->vm_start,