vmacache.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2014 Davidlohr Bueso.
  3. */
  4. #include <linux/sched.h>
  5. #include <linux/mm.h>
  6. #include <linux/vmacache.h>
  7. /*
  8. * Flush vma caches for threads that share a given mm.
  9. *
  10. * The operation is safe because the caller holds the mmap_sem
  11. * exclusively and other threads accessing the vma cache will
  12. * have mmap_sem held at least for read, so no extra locking
  13. * is required to maintain the vma cache.
  14. */
  15. void vmacache_flush_all(struct mm_struct *mm)
  16. {
  17. struct task_struct *g, *p;
  18. rcu_read_lock();
  19. for_each_process_thread(g, p) {
  20. /*
  21. * Only flush the vmacache pointers as the
  22. * mm seqnum is already set and curr's will
  23. * be set upon invalidation when the next
  24. * lookup is done.
  25. */
  26. if (mm == p->mm)
  27. vmacache_flush(p);
  28. }
  29. rcu_read_unlock();
  30. }
  31. /*
  32. * This task may be accessing a foreign mm via (for example)
  33. * get_user_pages()->find_vma(). The vmacache is task-local and this
  34. * task's vmacache pertains to a different mm (ie, its own). There is
  35. * nothing we can do here.
  36. *
  37. * Also handle the case where a kernel thread has adopted this mm via use_mm().
  38. * That kernel thread's vmacache is not applicable to this mm.
  39. */
  40. static bool vmacache_valid_mm(struct mm_struct *mm)
  41. {
  42. return current->mm == mm && !(current->flags & PF_KTHREAD);
  43. }
  44. void vmacache_update(unsigned long addr, struct vm_area_struct *newvma)
  45. {
  46. if (vmacache_valid_mm(newvma->vm_mm))
  47. current->vmacache[VMACACHE_HASH(addr)] = newvma;
  48. }
  49. static bool vmacache_valid(struct mm_struct *mm)
  50. {
  51. struct task_struct *curr;
  52. if (!vmacache_valid_mm(mm))
  53. return false;
  54. curr = current;
  55. if (mm->vmacache_seqnum != curr->vmacache_seqnum) {
  56. /*
  57. * First attempt will always be invalid, initialize
  58. * the new cache for this task here.
  59. */
  60. curr->vmacache_seqnum = mm->vmacache_seqnum;
  61. vmacache_flush(curr);
  62. return false;
  63. }
  64. return true;
  65. }
  66. struct vm_area_struct *vmacache_find(struct mm_struct *mm, unsigned long addr)
  67. {
  68. int i;
  69. if (!vmacache_valid(mm))
  70. return NULL;
  71. for (i = 0; i < VMACACHE_SIZE; i++) {
  72. struct vm_area_struct *vma = current->vmacache[i];
  73. if (!vma)
  74. continue;
  75. if (WARN_ON_ONCE(vma->vm_mm != mm))
  76. break;
  77. if (vma->vm_start <= addr && vma->vm_end > addr)
  78. return vma;
  79. }
  80. return NULL;
  81. }
  82. #ifndef CONFIG_MMU
  83. struct vm_area_struct *vmacache_find_exact(struct mm_struct *mm,
  84. unsigned long start,
  85. unsigned long end)
  86. {
  87. int i;
  88. if (!vmacache_valid(mm))
  89. return NULL;
  90. for (i = 0; i < VMACACHE_SIZE; i++) {
  91. struct vm_area_struct *vma = current->vmacache[i];
  92. if (vma && vma->vm_start == start && vma->vm_end == end)
  93. return vma;
  94. }
  95. return NULL;
  96. }
  97. #endif