net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.git] / mm / mmu_notifier.c
index 8d032de..a160ec8 100644 (file)
 
 #include <linux/rculist.h>
 #include <linux/mmu_notifier.h>
-#include <linux/module.h>
+#include <linux/export.h>
 #include <linux/mm.h>
 #include <linux/err.h>
+#include <linux/srcu.h>
 #include <linux/rcupdate.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
 
+/* global SRCU for all MMs */
+static struct srcu_struct srcu;
+
 /*
  * This function can't run concurrently against mmu_notifier_register
  * because mm->mm_users > 0 during mmu_notifier_register and exit_mmap
  * in parallel despite there being no task using this mm any more,
  * through the vmas outside of the exit_mmap context, such as with
  * vmtruncate. This serializes against mmu_notifier_unregister with
- * the mmu_notifier_mm->lock in addition to RCU and it serializes
- * against the other mmu notifiers with RCU. struct mmu_notifier_mm
+ * the mmu_notifier_mm->lock in addition to SRCU and it serializes
+ * against the other mmu notifiers with SRCU. struct mmu_notifier_mm
  * can't go away from under us as exit_mmap holds an mm_count pin
  * itself.
  */
 void __mmu_notifier_release(struct mm_struct *mm)
 {
        struct mmu_notifier *mn;
+       struct hlist_node *n;
+       int id;
+
+       /*
+        * SRCU here will block mmu_notifier_unregister until
+        * ->release returns.
+        */
+       id = srcu_read_lock(&srcu);
+       hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist)
+               /*
+                * If ->release runs before mmu_notifier_unregister it must be
+                * handled, as it's the only way for the driver to flush all
+                * existing sptes and stop the driver from establishing any more
+                * sptes before all the pages in the mm are freed.
+                */
+               if (mn->ops->release)
+                       mn->ops->release(mn, mm);
+       srcu_read_unlock(&srcu, id);
 
        spin_lock(&mm->mmu_notifier_mm->lock);
        while (unlikely(!hlist_empty(&mm->mmu_notifier_mm->list))) {
@@ -41,41 +63,24 @@ void __mmu_notifier_release(struct mm_struct *mm)
                                 hlist);
                /*
                 * We arrived before mmu_notifier_unregister so
-                * mmu_notifier_unregister will do nothing other than
-                * to wait ->release to finish and
-                * mmu_notifier_unregister to return.
+                * mmu_notifier_unregister will do nothing other than to wait
+                * for ->release to finish and for mmu_notifier_unregister to
+                * return.
                 */
                hlist_del_init_rcu(&mn->hlist);
-               /*
-                * RCU here will block mmu_notifier_unregister until
-                * ->release returns.
-                */
-               rcu_read_lock();
-               spin_unlock(&mm->mmu_notifier_mm->lock);
-               /*
-                * if ->release runs before mmu_notifier_unregister it
-                * must be handled as it's the only way for the driver
-                * to flush all existing sptes and stop the driver
-                * from establishing any more sptes before all the
-                * pages in the mm are freed.
-                */
-               if (mn->ops->release)
-                       mn->ops->release(mn, mm);
-               rcu_read_unlock();
-               spin_lock(&mm->mmu_notifier_mm->lock);
        }
        spin_unlock(&mm->mmu_notifier_mm->lock);
 
        /*
-        * synchronize_rcu here prevents mmu_notifier_release to
-        * return to exit_mmap (which would proceed freeing all pages
-        * in the mm) until the ->release method returns, if it was
-        * invoked by mmu_notifier_unregister.
+        * synchronize_srcu here prevents mmu_notifier_release from returning to
+        * exit_mmap (which would proceed with freeing all pages in the mm)
+        * until the ->release method returns, if it was invoked by
+        * mmu_notifier_unregister.
         *
-        * The mmu_notifier_mm can't go away from under us because one
-        * mm_count is hold by exit_mmap.
+        * The mmu_notifier_mm can't go away from under us because one mm_count
+        * is held by exit_mmap.
         */
-       synchronize_rcu();
+       synchronize_srcu(&srcu);
 }
 
 /*
@@ -88,14 +93,14 @@ int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
 {
        struct mmu_notifier *mn;
        struct hlist_node *n;
-       int young = 0;
+       int young = 0, id;
 
-       rcu_read_lock();
+       id = srcu_read_lock(&srcu);
        hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
                if (mn->ops->clear_flush_young)
                        young |= mn->ops->clear_flush_young(mn, mm, address);
        }
-       rcu_read_unlock();
+       srcu_read_unlock(&srcu, id);
 
        return young;
 }
@@ -105,9 +110,9 @@ int __mmu_notifier_test_young(struct mm_struct *mm,
 {
        struct mmu_notifier *mn;
        struct hlist_node *n;
-       int young = 0;
+       int young = 0, id;
 
-       rcu_read_lock();
+       id = srcu_read_lock(&srcu);
        hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
                if (mn->ops->test_young) {
                        young = mn->ops->test_young(mn, mm, address);
@@ -115,7 +120,7 @@ int __mmu_notifier_test_young(struct mm_struct *mm,
                                break;
                }
        }
-       rcu_read_unlock();
+       srcu_read_unlock(&srcu, id);
 
        return young;
 }
@@ -125,8 +130,9 @@ void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address,
 {
        struct mmu_notifier *mn;
        struct hlist_node *n;
+       int id;
 
-       rcu_read_lock();
+       id = srcu_read_lock(&srcu);
        hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
                if (mn->ops->change_pte)
                        mn->ops->change_pte(mn, mm, address, pte);
@@ -137,7 +143,7 @@ void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address,
                else if (mn->ops->invalidate_page)
                        mn->ops->invalidate_page(mn, mm, address);
        }
-       rcu_read_unlock();
+       srcu_read_unlock(&srcu, id);
 }
 
 void __mmu_notifier_invalidate_page(struct mm_struct *mm,
@@ -145,13 +151,14 @@ void __mmu_notifier_invalidate_page(struct mm_struct *mm,
 {
        struct mmu_notifier *mn;
        struct hlist_node *n;
+       int id;
 
-       rcu_read_lock();
+       id = srcu_read_lock(&srcu);
        hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
                if (mn->ops->invalidate_page)
                        mn->ops->invalidate_page(mn, mm, address);
        }
-       rcu_read_unlock();
+       srcu_read_unlock(&srcu, id);
 }
 
 void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
@@ -159,13 +166,14 @@ void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
 {
        struct mmu_notifier *mn;
        struct hlist_node *n;
+       int id;
 
-       rcu_read_lock();
+       id = srcu_read_lock(&srcu);
        hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
                if (mn->ops->invalidate_range_start)
                        mn->ops->invalidate_range_start(mn, mm, start, end);
        }
-       rcu_read_unlock();
+       srcu_read_unlock(&srcu, id);
 }
 
 void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
@@ -173,13 +181,14 @@ void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
 {
        struct mmu_notifier *mn;
        struct hlist_node *n;
+       int id;
 
-       rcu_read_lock();
+       id = srcu_read_lock(&srcu);
        hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
                if (mn->ops->invalidate_range_end)
                        mn->ops->invalidate_range_end(mn, mm, start, end);
        }
-       rcu_read_unlock();
+       srcu_read_unlock(&srcu, id);
 }
 
 static int do_mmu_notifier_register(struct mmu_notifier *mn,
@@ -191,6 +200,12 @@ static int do_mmu_notifier_register(struct mmu_notifier *mn,
 
        BUG_ON(atomic_read(&mm->mm_users) <= 0);
 
+       /*
+       * Verify that mmu_notifier_init() already run and the global srcu is
+       * initialized.
+       */
+       BUG_ON(!srcu.per_cpu_ref);
+
        ret = -ENOMEM;
        mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
        if (unlikely(!mmu_notifier_mm))
@@ -273,8 +288,8 @@ void __mmu_notifier_mm_destroy(struct mm_struct *mm)
 /*
  * This releases the mm_count pin automatically and frees the mm
  * structure if it was the last user of it. It serializes against
- * running mmu notifiers with RCU and against mmu_notifier_unregister
- * with the unregister lock + RCU. All sptes must be dropped before
+ * running mmu notifiers with SRCU and against mmu_notifier_unregister
+ * with the unregister lock + SRCU. All sptes must be dropped before
  * calling mmu_notifier_unregister. ->release or any other notifier
  * method may be invoked concurrently with mmu_notifier_unregister,
  * and only after mmu_notifier_unregister returned we're guaranteed
@@ -284,35 +299,46 @@ void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
 {
        BUG_ON(atomic_read(&mm->mm_count) <= 0);
 
-       spin_lock(&mm->mmu_notifier_mm->lock);
        if (!hlist_unhashed(&mn->hlist)) {
-               hlist_del_rcu(&mn->hlist);
-
                /*
-                * RCU here will force exit_mmap to wait ->release to finish
-                * before freeing the pages.
+                * SRCU here will force exit_mmap to wait for ->release to
+                * finish before freeing the pages.
                 */
-               rcu_read_lock();
-               spin_unlock(&mm->mmu_notifier_mm->lock);
+               int id;
+
+               id = srcu_read_lock(&srcu);
                /*
-                * exit_mmap will block in mmu_notifier_release to
-                * guarantee ->release is called before freeing the
-                * pages.
+                * exit_mmap will block in mmu_notifier_release to guarantee
+                * that ->release is called before freeing the pages.
                 */
                if (mn->ops->release)
                        mn->ops->release(mn, mm);
-               rcu_read_unlock();
-       } else
+               srcu_read_unlock(&srcu, id);
+
+               spin_lock(&mm->mmu_notifier_mm->lock);
+               /*
+                * Can not use list_del_rcu() since __mmu_notifier_release
+                * can delete it before we hold the lock.
+                */
+               hlist_del_init_rcu(&mn->hlist);
                spin_unlock(&mm->mmu_notifier_mm->lock);
+       }
 
        /*
-        * Wait any running method to finish, of course including
+        * Wait for any running method to finish, of course including
         * ->release if it was run by mmu_notifier_relase instead of us.
         */
-       synchronize_rcu();
+       synchronize_srcu(&srcu);
 
        BUG_ON(atomic_read(&mm->mm_count) <= 0);
 
        mmdrop(mm);
 }
 EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
+
+static int __init mmu_notifier_init(void)
+{
+       return init_srcu_struct(&srcu);
+}
+
+module_init(mmu_notifier_init);