Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/mm/mmu_notifier.c
0003  *
0004  *  Copyright (C) 2008  Qumranet, Inc.
0005  *  Copyright (C) 2008  SGI
0006  *             Christoph Lameter <cl@linux.com>
0007  *
0008  *  This work is licensed under the terms of the GNU GPL, version 2. See
0009  *  the COPYING file in the top-level directory.
0010  */
0011 
0012 #include <linux/rculist.h>
0013 #include <linux/mmu_notifier.h>
0014 #include <linux/export.h>
0015 #include <linux/mm.h>
0016 #include <linux/err.h>
0017 #include <linux/srcu.h>
0018 #include <linux/rcupdate.h>
0019 #include <linux/sched.h>
0020 #include <linux/slab.h>
0021 
0022 /* global SRCU for all MMs */
0023 static struct srcu_struct srcu;
0024 
0025 /*
0026  * This function allows mmu_notifier::release callback to delay a call to
0027  * a function that will free appropriate resources. The function must be
0028  * quick and must not block.
0029  */
0030 void mmu_notifier_call_srcu(struct rcu_head *rcu,
0031                 void (*func)(struct rcu_head *rcu))
0032 {
0033     call_srcu(&srcu, rcu, func);
0034 }
0035 EXPORT_SYMBOL_GPL(mmu_notifier_call_srcu);
0036 
0037 void mmu_notifier_synchronize(void)
0038 {
0039     /* Wait for any running method to finish. */
0040     srcu_barrier(&srcu);
0041 }
0042 EXPORT_SYMBOL_GPL(mmu_notifier_synchronize);
0043 
0044 /*
0045  * This function can't run concurrently against mmu_notifier_register
0046  * because mm->mm_users > 0 during mmu_notifier_register and exit_mmap
0047  * runs with mm_users == 0. Other tasks may still invoke mmu notifiers
0048  * in parallel despite there being no task using this mm any more,
0049  * through the vmas outside of the exit_mmap context, such as with
0050  * vmtruncate. This serializes against mmu_notifier_unregister with
0051  * the mmu_notifier_mm->lock in addition to SRCU and it serializes
0052  * against the other mmu notifiers with SRCU. struct mmu_notifier_mm
0053  * can't go away from under us as exit_mmap holds an mm_count pin
0054  * itself.
0055  */
0056 void __mmu_notifier_release(struct mm_struct *mm)
0057 {
0058     struct mmu_notifier *mn;
0059     int id;
0060 
0061     /*
0062      * SRCU here will block mmu_notifier_unregister until
0063      * ->release returns.
0064      */
0065     id = srcu_read_lock(&srcu);
0066     hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist)
0067         /*
0068          * If ->release runs before mmu_notifier_unregister it must be
0069          * handled, as it's the only way for the driver to flush all
0070          * existing sptes and stop the driver from establishing any more
0071          * sptes before all the pages in the mm are freed.
0072          */
0073         if (mn->ops->release)
0074             mn->ops->release(mn, mm);
0075 
0076     spin_lock(&mm->mmu_notifier_mm->lock);
0077     while (unlikely(!hlist_empty(&mm->mmu_notifier_mm->list))) {
0078         mn = hlist_entry(mm->mmu_notifier_mm->list.first,
0079                  struct mmu_notifier,
0080                  hlist);
0081         /*
0082          * We arrived before mmu_notifier_unregister so
0083          * mmu_notifier_unregister will do nothing other than to wait
0084          * for ->release to finish and for mmu_notifier_unregister to
0085          * return.
0086          */
0087         hlist_del_init_rcu(&mn->hlist);
0088     }
0089     spin_unlock(&mm->mmu_notifier_mm->lock);
0090     srcu_read_unlock(&srcu, id);
0091 
0092     /*
0093      * synchronize_srcu here prevents mmu_notifier_release from returning to
0094      * exit_mmap (which would proceed with freeing all pages in the mm)
0095      * until the ->release method returns, if it was invoked by
0096      * mmu_notifier_unregister.
0097      *
0098      * The mmu_notifier_mm can't go away from under us because one mm_count
0099      * is held by exit_mmap.
0100      */
0101     synchronize_srcu(&srcu);
0102 }
0103 
0104 /*
0105  * If no young bitflag is supported by the hardware, ->clear_flush_young can
0106  * unmap the address and return 1 or 0 depending if the mapping previously
0107  * existed or not.
0108  */
0109 int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
0110                     unsigned long start,
0111                     unsigned long end)
0112 {
0113     struct mmu_notifier *mn;
0114     int young = 0, id;
0115 
0116     id = srcu_read_lock(&srcu);
0117     hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
0118         if (mn->ops->clear_flush_young)
0119             young |= mn->ops->clear_flush_young(mn, mm, start, end);
0120     }
0121     srcu_read_unlock(&srcu, id);
0122 
0123     return young;
0124 }
0125 
0126 int __mmu_notifier_clear_young(struct mm_struct *mm,
0127                    unsigned long start,
0128                    unsigned long end)
0129 {
0130     struct mmu_notifier *mn;
0131     int young = 0, id;
0132 
0133     id = srcu_read_lock(&srcu);
0134     hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
0135         if (mn->ops->clear_young)
0136             young |= mn->ops->clear_young(mn, mm, start, end);
0137     }
0138     srcu_read_unlock(&srcu, id);
0139 
0140     return young;
0141 }
0142 
0143 int __mmu_notifier_test_young(struct mm_struct *mm,
0144                   unsigned long address)
0145 {
0146     struct mmu_notifier *mn;
0147     int young = 0, id;
0148 
0149     id = srcu_read_lock(&srcu);
0150     hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
0151         if (mn->ops->test_young) {
0152             young = mn->ops->test_young(mn, mm, address);
0153             if (young)
0154                 break;
0155         }
0156     }
0157     srcu_read_unlock(&srcu, id);
0158 
0159     return young;
0160 }
0161 
0162 void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address,
0163                    pte_t pte)
0164 {
0165     struct mmu_notifier *mn;
0166     int id;
0167 
0168     id = srcu_read_lock(&srcu);
0169     hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
0170         if (mn->ops->change_pte)
0171             mn->ops->change_pte(mn, mm, address, pte);
0172     }
0173     srcu_read_unlock(&srcu, id);
0174 }
0175 
0176 void __mmu_notifier_invalidate_page(struct mm_struct *mm,
0177                       unsigned long address)
0178 {
0179     struct mmu_notifier *mn;
0180     int id;
0181 
0182     id = srcu_read_lock(&srcu);
0183     hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
0184         if (mn->ops->invalidate_page)
0185             mn->ops->invalidate_page(mn, mm, address);
0186     }
0187     srcu_read_unlock(&srcu, id);
0188 }
0189 
0190 void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
0191                   unsigned long start, unsigned long end)
0192 {
0193     struct mmu_notifier *mn;
0194     int id;
0195 
0196     id = srcu_read_lock(&srcu);
0197     hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
0198         if (mn->ops->invalidate_range_start)
0199             mn->ops->invalidate_range_start(mn, mm, start, end);
0200     }
0201     srcu_read_unlock(&srcu, id);
0202 }
0203 EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_start);
0204 
0205 void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
0206                   unsigned long start, unsigned long end)
0207 {
0208     struct mmu_notifier *mn;
0209     int id;
0210 
0211     id = srcu_read_lock(&srcu);
0212     hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
0213         /*
0214          * Call invalidate_range here too to avoid the need for the
0215          * subsystem of having to register an invalidate_range_end
0216          * call-back when there is invalidate_range already. Usually a
0217          * subsystem registers either invalidate_range_start()/end() or
0218          * invalidate_range(), so this will be no additional overhead
0219          * (besides the pointer check).
0220          */
0221         if (mn->ops->invalidate_range)
0222             mn->ops->invalidate_range(mn, mm, start, end);
0223         if (mn->ops->invalidate_range_end)
0224             mn->ops->invalidate_range_end(mn, mm, start, end);
0225     }
0226     srcu_read_unlock(&srcu, id);
0227 }
0228 EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_end);
0229 
0230 void __mmu_notifier_invalidate_range(struct mm_struct *mm,
0231                   unsigned long start, unsigned long end)
0232 {
0233     struct mmu_notifier *mn;
0234     int id;
0235 
0236     id = srcu_read_lock(&srcu);
0237     hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
0238         if (mn->ops->invalidate_range)
0239             mn->ops->invalidate_range(mn, mm, start, end);
0240     }
0241     srcu_read_unlock(&srcu, id);
0242 }
0243 EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range);
0244 
0245 static int do_mmu_notifier_register(struct mmu_notifier *mn,
0246                     struct mm_struct *mm,
0247                     int take_mmap_sem)
0248 {
0249     struct mmu_notifier_mm *mmu_notifier_mm;
0250     int ret;
0251 
0252     BUG_ON(atomic_read(&mm->mm_users) <= 0);
0253 
0254     /*
0255      * Verify that mmu_notifier_init() already run and the global srcu is
0256      * initialized.
0257      */
0258     BUG_ON(!srcu.per_cpu_ref);
0259 
0260     ret = -ENOMEM;
0261     mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
0262     if (unlikely(!mmu_notifier_mm))
0263         goto out;
0264 
0265     if (take_mmap_sem)
0266         down_write(&mm->mmap_sem);
0267     ret = mm_take_all_locks(mm);
0268     if (unlikely(ret))
0269         goto out_clean;
0270 
0271     if (!mm_has_notifiers(mm)) {
0272         INIT_HLIST_HEAD(&mmu_notifier_mm->list);
0273         spin_lock_init(&mmu_notifier_mm->lock);
0274 
0275         mm->mmu_notifier_mm = mmu_notifier_mm;
0276         mmu_notifier_mm = NULL;
0277     }
0278     atomic_inc(&mm->mm_count);
0279 
0280     /*
0281      * Serialize the update against mmu_notifier_unregister. A
0282      * side note: mmu_notifier_release can't run concurrently with
0283      * us because we hold the mm_users pin (either implicitly as
0284      * current->mm or explicitly with get_task_mm() or similar).
0285      * We can't race against any other mmu notifier method either
0286      * thanks to mm_take_all_locks().
0287      */
0288     spin_lock(&mm->mmu_notifier_mm->lock);
0289     hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list);
0290     spin_unlock(&mm->mmu_notifier_mm->lock);
0291 
0292     mm_drop_all_locks(mm);
0293 out_clean:
0294     if (take_mmap_sem)
0295         up_write(&mm->mmap_sem);
0296     kfree(mmu_notifier_mm);
0297 out:
0298     BUG_ON(atomic_read(&mm->mm_users) <= 0);
0299     return ret;
0300 }
0301 
0302 /*
0303  * Must not hold mmap_sem nor any other VM related lock when calling
0304  * this registration function. Must also ensure mm_users can't go down
0305  * to zero while this runs to avoid races with mmu_notifier_release,
0306  * so mm has to be current->mm or the mm should be pinned safely such
0307  * as with get_task_mm(). If the mm is not current->mm, the mm_users
0308  * pin should be released by calling mmput after mmu_notifier_register
0309  * returns. mmu_notifier_unregister must be always called to
0310  * unregister the notifier. mm_count is automatically pinned to allow
0311  * mmu_notifier_unregister to safely run at any time later, before or
0312  * after exit_mmap. ->release will always be called before exit_mmap
0313  * frees the pages.
0314  */
0315 int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
0316 {
0317     return do_mmu_notifier_register(mn, mm, 1);
0318 }
0319 EXPORT_SYMBOL_GPL(mmu_notifier_register);
0320 
0321 /*
0322  * Same as mmu_notifier_register but here the caller must hold the
0323  * mmap_sem in write mode.
0324  */
0325 int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
0326 {
0327     return do_mmu_notifier_register(mn, mm, 0);
0328 }
0329 EXPORT_SYMBOL_GPL(__mmu_notifier_register);
0330 
0331 /* this is called after the last mmu_notifier_unregister() returned */
0332 void __mmu_notifier_mm_destroy(struct mm_struct *mm)
0333 {
0334     BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list));
0335     kfree(mm->mmu_notifier_mm);
0336     mm->mmu_notifier_mm = LIST_POISON1; /* debug */
0337 }
0338 
0339 /*
0340  * This releases the mm_count pin automatically and frees the mm
0341  * structure if it was the last user of it. It serializes against
0342  * running mmu notifiers with SRCU and against mmu_notifier_unregister
0343  * with the unregister lock + SRCU. All sptes must be dropped before
0344  * calling mmu_notifier_unregister. ->release or any other notifier
0345  * method may be invoked concurrently with mmu_notifier_unregister,
0346  * and only after mmu_notifier_unregister returned we're guaranteed
0347  * that ->release or any other method can't run anymore.
0348  */
0349 void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
0350 {
0351     BUG_ON(atomic_read(&mm->mm_count) <= 0);
0352 
0353     if (!hlist_unhashed(&mn->hlist)) {
0354         /*
0355          * SRCU here will force exit_mmap to wait for ->release to
0356          * finish before freeing the pages.
0357          */
0358         int id;
0359 
0360         id = srcu_read_lock(&srcu);
0361         /*
0362          * exit_mmap will block in mmu_notifier_release to guarantee
0363          * that ->release is called before freeing the pages.
0364          */
0365         if (mn->ops->release)
0366             mn->ops->release(mn, mm);
0367         srcu_read_unlock(&srcu, id);
0368 
0369         spin_lock(&mm->mmu_notifier_mm->lock);
0370         /*
0371          * Can not use list_del_rcu() since __mmu_notifier_release
0372          * can delete it before we hold the lock.
0373          */
0374         hlist_del_init_rcu(&mn->hlist);
0375         spin_unlock(&mm->mmu_notifier_mm->lock);
0376     }
0377 
0378     /*
0379      * Wait for any running method to finish, of course including
0380      * ->release if it was run by mmu_notifier_release instead of us.
0381      */
0382     synchronize_srcu(&srcu);
0383 
0384     BUG_ON(atomic_read(&mm->mm_count) <= 0);
0385 
0386     mmdrop(mm);
0387 }
0388 EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
0389 
0390 /*
0391  * Same as mmu_notifier_unregister but no callback and no srcu synchronization.
0392  */
0393 void mmu_notifier_unregister_no_release(struct mmu_notifier *mn,
0394                     struct mm_struct *mm)
0395 {
0396     spin_lock(&mm->mmu_notifier_mm->lock);
0397     /*
0398      * Can not use list_del_rcu() since __mmu_notifier_release
0399      * can delete it before we hold the lock.
0400      */
0401     hlist_del_init_rcu(&mn->hlist);
0402     spin_unlock(&mm->mmu_notifier_mm->lock);
0403 
0404     BUG_ON(atomic_read(&mm->mm_count) <= 0);
0405     mmdrop(mm);
0406 }
0407 EXPORT_SYMBOL_GPL(mmu_notifier_unregister_no_release);
0408 
0409 static int __init mmu_notifier_init(void)
0410 {
0411     return init_srcu_struct(&srcu);
0412 }
0413 subsys_initcall(mmu_notifier_init);