vmacache.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. count_vm_vmacache_event(VMACACHE_FULL_FLUSHES);
  19. /*
  20. * Single threaded tasks need not iterate the entire
  21. * list of process. We can avoid the flushing as well
  22. * since the mm's seqnum was increased and don't have
  23. * to worry about other threads' seqnum. Current's
  24. * flush will occur upon the next lookup.
  25. */
  26. if (atomic_read(&mm->mm_users) == 1)
  27. return;
  28. rcu_read_lock();
  29. for_each_process_thread(g, p) {
  30. /*
  31. * Only flush the vmacache pointers as the
  32. * mm seqnum is already set and curr's will
  33. * be set upon invalidation when the next
  34. * lookup is done.
  35. */
  36. if (mm == p->mm)
  37. vmacache_flush(p);
  38. }
  39. rcu_read_unlock();
  40. }
  41. /*
  42. * This task may be accessing a foreign mm via (for example)
  43. * get_user_pages()->find_vma(). The vmacache is task-local and this
  44. * task's vmacache pertains to a different mm (ie, its own). There is
  45. * nothing we can do here.
  46. *
  47. * Also handle the case where a kernel thread has adopted this mm via use_mm().
  48. * That kernel thread's vmacache is not applicable to this mm.
  49. */
  50. static inline bool vmacache_valid_mm(struct mm_struct *mm)
  51. {
  52. return current->mm == mm && !(current->flags & PF_KTHREAD);
  53. }
  54. void vmacache_update(unsigned long addr, struct vm_area_struct *newvma)
  55. {
  56. if (vmacache_valid_mm(newvma->vm_mm))
  57. current->vmacache[VMACACHE_HASH(addr)] = newvma;
  58. }
  59. static bool vmacache_valid(struct mm_struct *mm)
  60. {
  61. struct task_struct *curr;
  62. if (!vmacache_valid_mm(mm))
  63. return false;
  64. curr = current;
  65. if (mm->vmacache_seqnum != curr->vmacache_seqnum) {
  66. /*
  67. * First attempt will always be invalid, initialize
  68. * the new cache for this task here.
  69. */
  70. curr->vmacache_seqnum = mm->vmacache_seqnum;
  71. vmacache_flush(curr);
  72. return false;
  73. }
  74. return true;
  75. }
  76. struct vm_area_struct *vmacache_find(struct mm_struct *mm, unsigned long addr)
  77. {
  78. int i;
  79. count_vm_vmacache_event(VMACACHE_FIND_CALLS);
  80. if (!vmacache_valid(mm))
  81. return NULL;
  82. for (i = 0; i < VMACACHE_SIZE; i++) {
  83. struct vm_area_struct *vma = current->vmacache[i];
  84. if (!vma)
  85. continue;
  86. if (WARN_ON_ONCE(vma->vm_mm != mm))
  87. break;
  88. if (vma->vm_start <= addr && vma->vm_end > addr) {
  89. count_vm_vmacache_event(VMACACHE_FIND_HITS);
  90. return vma;
  91. }
  92. }
  93. return NULL;
  94. }
  95. #ifndef CONFIG_MMU
  96. struct vm_area_struct *vmacache_find_exact(struct mm_struct *mm,
  97. unsigned long start,
  98. unsigned long end)
  99. {
  100. int i;
  101. count_vm_vmacache_event(VMACACHE_FIND_CALLS);
  102. if (!vmacache_valid(mm))
  103. return NULL;
  104. for (i = 0; i < VMACACHE_SIZE; i++) {
  105. struct vm_area_struct *vma = current->vmacache[i];
  106. if (vma && vma->vm_start == start && vma->vm_end == end) {
  107. count_vm_vmacache_event(VMACACHE_FIND_HITS);
  108. return vma;
  109. }
  110. }
  111. return NULL;
  112. }
  113. #endif