vmacache.c 3.1 KB

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