mempolicy: fix a memory corruption by refcount imbalance in alloc_pages_vma()
[firefly-linux-kernel-4.4.55.git] / mm / mempolicy.c
index 1763418f1adad41450f6d766efd8173d08312b0b..3d64b369180d603225ea92348a6e5d0e10a8a06c 100644 (file)
@@ -1552,8 +1552,18 @@ struct mempolicy *get_vma_policy(struct task_struct *task,
                                                                        addr);
                        if (vpol)
                                pol = vpol;
-               } else if (vma->vm_policy)
+               } else if (vma->vm_policy) {
                        pol = vma->vm_policy;
+
+                       /*
+                        * shmem_alloc_page() passes MPOL_F_SHARED policy with
+                        * a pseudo vma whose vma->vm_ops=NULL. Take a reference
+                        * count on these policies which will be dropped by
+                        * mpol_cond_put() later
+                        */
+                       if (mpol_needs_cond_ref(pol))
+                               mpol_get(pol);
+               }
        }
        if (!pol)
                pol = &default_policy;