/*1* linux/mm/mmu_notifier.c2*3* Copyright (C) 2008 Qumranet, Inc.4* Copyright (C) 2008 SGI5* Christoph Lameter <[email protected]>6*7* This work is licensed under the terms of the GNU GPL, version 2. See8* the COPYING file in the top-level directory.9*/1011#include <linux/rculist.h>12#include <linux/mmu_notifier.h>13#include <linux/module.h>14#include <linux/mm.h>15#include <linux/err.h>16#include <linux/rcupdate.h>17#include <linux/sched.h>18#include <linux/slab.h>1920/*21* This function can't run concurrently against mmu_notifier_register22* because mm->mm_users > 0 during mmu_notifier_register and exit_mmap23* runs with mm_users == 0. Other tasks may still invoke mmu notifiers24* in parallel despite there being no task using this mm any more,25* through the vmas outside of the exit_mmap context, such as with26* vmtruncate. This serializes against mmu_notifier_unregister with27* the mmu_notifier_mm->lock in addition to RCU and it serializes28* against the other mmu notifiers with RCU. struct mmu_notifier_mm29* can't go away from under us as exit_mmap holds an mm_count pin30* itself.31*/32void __mmu_notifier_release(struct mm_struct *mm)33{34struct mmu_notifier *mn;3536spin_lock(&mm->mmu_notifier_mm->lock);37while (unlikely(!hlist_empty(&mm->mmu_notifier_mm->list))) {38mn = hlist_entry(mm->mmu_notifier_mm->list.first,39struct mmu_notifier,40hlist);41/*42* We arrived before mmu_notifier_unregister so43* mmu_notifier_unregister will do nothing other than44* to wait ->release to finish and45* mmu_notifier_unregister to return.46*/47hlist_del_init_rcu(&mn->hlist);48/*49* RCU here will block mmu_notifier_unregister until50* ->release returns.51*/52rcu_read_lock();53spin_unlock(&mm->mmu_notifier_mm->lock);54/*55* if ->release runs before mmu_notifier_unregister it56* must be handled as it's the only way for the driver57* to flush all existing sptes and stop the driver58* from establishing any more sptes before all the59* pages in the mm are freed.60*/61if (mn->ops->release)62mn->ops->release(mn, mm);63rcu_read_unlock();64spin_lock(&mm->mmu_notifier_mm->lock);65}66spin_unlock(&mm->mmu_notifier_mm->lock);6768/*69* synchronize_rcu here prevents mmu_notifier_release to70* return to exit_mmap (which would proceed freeing all pages71* in the mm) until the ->release method returns, if it was72* invoked by mmu_notifier_unregister.73*74* The mmu_notifier_mm can't go away from under us because one75* mm_count is hold by exit_mmap.76*/77synchronize_rcu();78}7980/*81* If no young bitflag is supported by the hardware, ->clear_flush_young can82* unmap the address and return 1 or 0 depending if the mapping previously83* existed or not.84*/85int __mmu_notifier_clear_flush_young(struct mm_struct *mm,86unsigned long address)87{88struct mmu_notifier *mn;89struct hlist_node *n;90int young = 0;9192rcu_read_lock();93hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {94if (mn->ops->clear_flush_young)95young |= mn->ops->clear_flush_young(mn, mm, address);96}97rcu_read_unlock();9899return young;100}101102int __mmu_notifier_test_young(struct mm_struct *mm,103unsigned long address)104{105struct mmu_notifier *mn;106struct hlist_node *n;107int young = 0;108109rcu_read_lock();110hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {111if (mn->ops->test_young) {112young = mn->ops->test_young(mn, mm, address);113if (young)114break;115}116}117rcu_read_unlock();118119return young;120}121122void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address,123pte_t pte)124{125struct mmu_notifier *mn;126struct hlist_node *n;127128rcu_read_lock();129hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {130if (mn->ops->change_pte)131mn->ops->change_pte(mn, mm, address, pte);132/*133* Some drivers don't have change_pte,134* so we must call invalidate_page in that case.135*/136else if (mn->ops->invalidate_page)137mn->ops->invalidate_page(mn, mm, address);138}139rcu_read_unlock();140}141142void __mmu_notifier_invalidate_page(struct mm_struct *mm,143unsigned long address)144{145struct mmu_notifier *mn;146struct hlist_node *n;147148rcu_read_lock();149hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {150if (mn->ops->invalidate_page)151mn->ops->invalidate_page(mn, mm, address);152}153rcu_read_unlock();154}155156void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,157unsigned long start, unsigned long end)158{159struct mmu_notifier *mn;160struct hlist_node *n;161162rcu_read_lock();163hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {164if (mn->ops->invalidate_range_start)165mn->ops->invalidate_range_start(mn, mm, start, end);166}167rcu_read_unlock();168}169170void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,171unsigned long start, unsigned long end)172{173struct mmu_notifier *mn;174struct hlist_node *n;175176rcu_read_lock();177hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {178if (mn->ops->invalidate_range_end)179mn->ops->invalidate_range_end(mn, mm, start, end);180}181rcu_read_unlock();182}183184static int do_mmu_notifier_register(struct mmu_notifier *mn,185struct mm_struct *mm,186int take_mmap_sem)187{188struct mmu_notifier_mm *mmu_notifier_mm;189int ret;190191BUG_ON(atomic_read(&mm->mm_users) <= 0);192193ret = -ENOMEM;194mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);195if (unlikely(!mmu_notifier_mm))196goto out;197198if (take_mmap_sem)199down_write(&mm->mmap_sem);200ret = mm_take_all_locks(mm);201if (unlikely(ret))202goto out_cleanup;203204if (!mm_has_notifiers(mm)) {205INIT_HLIST_HEAD(&mmu_notifier_mm->list);206spin_lock_init(&mmu_notifier_mm->lock);207mm->mmu_notifier_mm = mmu_notifier_mm;208mmu_notifier_mm = NULL;209}210atomic_inc(&mm->mm_count);211212/*213* Serialize the update against mmu_notifier_unregister. A214* side note: mmu_notifier_release can't run concurrently with215* us because we hold the mm_users pin (either implicitly as216* current->mm or explicitly with get_task_mm() or similar).217* We can't race against any other mmu notifier method either218* thanks to mm_take_all_locks().219*/220spin_lock(&mm->mmu_notifier_mm->lock);221hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list);222spin_unlock(&mm->mmu_notifier_mm->lock);223224mm_drop_all_locks(mm);225out_cleanup:226if (take_mmap_sem)227up_write(&mm->mmap_sem);228/* kfree() does nothing if mmu_notifier_mm is NULL */229kfree(mmu_notifier_mm);230out:231BUG_ON(atomic_read(&mm->mm_users) <= 0);232return ret;233}234235/*236* Must not hold mmap_sem nor any other VM related lock when calling237* this registration function. Must also ensure mm_users can't go down238* to zero while this runs to avoid races with mmu_notifier_release,239* so mm has to be current->mm or the mm should be pinned safely such240* as with get_task_mm(). If the mm is not current->mm, the mm_users241* pin should be released by calling mmput after mmu_notifier_register242* returns. mmu_notifier_unregister must be always called to243* unregister the notifier. mm_count is automatically pinned to allow244* mmu_notifier_unregister to safely run at any time later, before or245* after exit_mmap. ->release will always be called before exit_mmap246* frees the pages.247*/248int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)249{250return do_mmu_notifier_register(mn, mm, 1);251}252EXPORT_SYMBOL_GPL(mmu_notifier_register);253254/*255* Same as mmu_notifier_register but here the caller must hold the256* mmap_sem in write mode.257*/258int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)259{260return do_mmu_notifier_register(mn, mm, 0);261}262EXPORT_SYMBOL_GPL(__mmu_notifier_register);263264/* this is called after the last mmu_notifier_unregister() returned */265void __mmu_notifier_mm_destroy(struct mm_struct *mm)266{267BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list));268kfree(mm->mmu_notifier_mm);269mm->mmu_notifier_mm = LIST_POISON1; /* debug */270}271272/*273* This releases the mm_count pin automatically and frees the mm274* structure if it was the last user of it. It serializes against275* running mmu notifiers with RCU and against mmu_notifier_unregister276* with the unregister lock + RCU. All sptes must be dropped before277* calling mmu_notifier_unregister. ->release or any other notifier278* method may be invoked concurrently with mmu_notifier_unregister,279* and only after mmu_notifier_unregister returned we're guaranteed280* that ->release or any other method can't run anymore.281*/282void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)283{284BUG_ON(atomic_read(&mm->mm_count) <= 0);285286spin_lock(&mm->mmu_notifier_mm->lock);287if (!hlist_unhashed(&mn->hlist)) {288hlist_del_rcu(&mn->hlist);289290/*291* RCU here will force exit_mmap to wait ->release to finish292* before freeing the pages.293*/294rcu_read_lock();295spin_unlock(&mm->mmu_notifier_mm->lock);296/*297* exit_mmap will block in mmu_notifier_release to298* guarantee ->release is called before freeing the299* pages.300*/301if (mn->ops->release)302mn->ops->release(mn, mm);303rcu_read_unlock();304} else305spin_unlock(&mm->mmu_notifier_mm->lock);306307/*308* Wait any running method to finish, of course including309* ->release if it was run by mmu_notifier_relase instead of us.310*/311synchronize_rcu();312313BUG_ON(atomic_read(&mm->mm_count) <= 0);314315mmdrop(mm);316}317EXPORT_SYMBOL_GPL(mmu_notifier_unregister);318319320