ksm: fix oom deadlock
[linux-2.6-block.git] / mm / mmap.c
index 376492ed08f4a66cbc782171cc0924adb1287018..e02f1aa66a1a8176aa953152bf40bc4112e3f26c 100644 (file)
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -27,6 +27,7 @@
 #include <linux/mount.h>
 #include <linux/mempolicy.h>
 #include <linux/rmap.h>
+#include <linux/ksm.h>
 #include <linux/mmu_notifier.h>
 #include <linux/perf_event.h>
 
@@ -2111,6 +2112,14 @@ void exit_mmap(struct mm_struct *mm)
        /* Use -1 here to ensure all VMAs in the mm are unmapped */
        end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
        vm_unacct_memory(nr_accounted);
+
+       /*
+        * For KSM to handle OOM without deadlock when it's breaking COW in a
+        * likely victim of the OOM killer, we must serialize with ksm_exit()
+        * after freeing mm's pages but before freeing its page tables.
+        */
+       ksm_exit(mm, &tlb, end);
+
        free_pgtables(tlb, vma, FIRST_USER_ADDRESS, 0);
        tlb_finish_mmu(tlb, 0, end);