vmacache.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Davidlohr Bueso.
  4. */
  5. #include <linux/sched/signal.h>
  6. #include <linux/sched/task.h>
  7. #include <linux/mm.h>
  8. #include <linux/vmacache.h>
  9. #include <asm/pgtable.h>
  10. /*
  11. * Hash based on the pmd of addr if configured with MMU, which provides a good
  12. * hit rate for workloads with spatial locality. Otherwise, use pages.
  13. */
  14. #ifdef CONFIG_MMU
  15. #define VMACACHE_SHIFT PMD_SHIFT
  16. #else
  17. #define VMACACHE_SHIFT PAGE_SHIFT
  18. #endif
  19. #define VMACACHE_HASH(addr) ((addr >> VMACACHE_SHIFT) & VMACACHE_MASK)
  20. /*
  21. * Flush vma caches for threads that share a given mm.
  22. *
  23. * The operation is safe because the caller holds the mmap_sem
  24. * exclusively and other threads accessing the vma cache will
  25. * have mmap_sem held at least for read, so no extra locking
  26. * is required to maintain the vma cache.
  27. */
  28. void vmacache_flush_all(struct mm_struct *mm)
  29. {
  30. struct task_struct *g, *p;
  31. count_vm_vmacache_event(VMACACHE_FULL_FLUSHES);
  32. /*
  33. * Single threaded tasks need not iterate the entire
  34. * list of process. We can avoid the flushing as well
  35. * since the mm's seqnum was increased and don't have
  36. * to worry about other threads' seqnum. Current's
  37. * flush will occur upon the next lookup.
  38. */
  39. if (atomic_read(&mm->mm_users) == 1)
  40. return;
  41. rcu_read_lock();
  42. for_each_process_thread(g, p) {
  43. /*
  44. * Only flush the vmacache pointers as the
  45. * mm seqnum is already set and curr's will
  46. * be set upon invalidation when the next
  47. * lookup is done.
  48. */
  49. if (mm == p->mm)
  50. vmacache_flush(p);
  51. }
  52. rcu_read_unlock();
  53. }
  54. /*
  55. * This task may be accessing a foreign mm via (for example)
  56. * get_user_pages()->find_vma(). The vmacache is task-local and this
  57. * task's vmacache pertains to a different mm (ie, its own). There is
  58. * nothing we can do here.
  59. *
  60. * Also handle the case where a kernel thread has adopted this mm via use_mm().
  61. * That kernel thread's vmacache is not applicable to this mm.
  62. */
  63. static inline bool vmacache_valid_mm(struct mm_struct *mm)
  64. {
  65. return current->mm == mm && !(current->flags & PF_KTHREAD);
  66. }
  67. void vmacache_update(unsigned long addr, struct vm_area_struct *newvma)
  68. {
  69. if (vmacache_valid_mm(newvma->vm_mm))
  70. current->vmacache.vmas[VMACACHE_HASH(addr)] = newvma;
  71. }
  72. static bool vmacache_valid(struct mm_struct *mm)
  73. {
  74. struct task_struct *curr;
  75. if (!vmacache_valid_mm(mm))
  76. return false;
  77. curr = current;
  78. if (mm->vmacache_seqnum != curr->vmacache.seqnum) {
  79. /*
  80. * First attempt will always be invalid, initialize
  81. * the new cache for this task here.
  82. */
  83. curr->vmacache.seqnum = mm->vmacache_seqnum;
  84. vmacache_flush(curr);
  85. return false;
  86. }
  87. return true;
  88. }
  89. struct vm_area_struct *vmacache_find(struct mm_struct *mm, unsigned long addr)
  90. {
  91. int idx = VMACACHE_HASH(addr);
  92. int i;
  93. count_vm_vmacache_event(VMACACHE_FIND_CALLS);
  94. if (!vmacache_valid(mm))
  95. return NULL;
  96. for (i = 0; i < VMACACHE_SIZE; i++) {
  97. struct vm_area_struct *vma = current->vmacache.vmas[idx];
  98. if (vma) {
  99. #ifdef CONFIG_DEBUG_VM_VMACACHE
  100. if (WARN_ON_ONCE(vma->vm_mm != mm))
  101. break;
  102. #endif
  103. if (vma->vm_start <= addr && vma->vm_end > addr) {
  104. count_vm_vmacache_event(VMACACHE_FIND_HITS);
  105. return vma;
  106. }
  107. }
  108. if (++idx == VMACACHE_SIZE)
  109. idx = 0;
  110. }
  111. return NULL;
  112. }
  113. #ifndef CONFIG_MMU
  114. struct vm_area_struct *vmacache_find_exact(struct mm_struct *mm,
  115. unsigned long start,
  116. unsigned long end)
  117. {
  118. int idx = VMACACHE_HASH(start);
  119. int i;
  120. count_vm_vmacache_event(VMACACHE_FIND_CALLS);
  121. if (!vmacache_valid(mm))
  122. return NULL;
  123. for (i = 0; i < VMACACHE_SIZE; i++) {
  124. struct vm_area_struct *vma = current->vmacache.vmas[idx];
  125. if (vma && vma->vm_start == start && vma->vm_end == end) {
  126. count_vm_vmacache_event(VMACACHE_FIND_HITS);
  127. return vma;
  128. }
  129. if (++idx == VMACACHE_SIZE)
  130. idx = 0;
  131. }
  132. return NULL;
  133. }
  134. #endif