vmacache.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * This task may be accessing a foreign mm via (for example)
  22. * get_user_pages()->find_vma(). The vmacache is task-local and this
  23. * task's vmacache pertains to a different mm (ie, its own). There is
  24. * nothing we can do here.
  25. *
  26. * Also handle the case where a kernel thread has adopted this mm via use_mm().
  27. * That kernel thread's vmacache is not applicable to this mm.
  28. */
  29. static inline bool vmacache_valid_mm(struct mm_struct *mm)
  30. {
  31. return current->mm == mm && !(current->flags & PF_KTHREAD);
  32. }
  33. void vmacache_update(unsigned long addr, struct vm_area_struct *newvma)
  34. {
  35. if (vmacache_valid_mm(newvma->vm_mm))
  36. current->vmacache.vmas[VMACACHE_HASH(addr)] = newvma;
  37. }
  38. static bool vmacache_valid(struct mm_struct *mm)
  39. {
  40. struct task_struct *curr;
  41. if (!vmacache_valid_mm(mm))
  42. return false;
  43. curr = current;
  44. if (mm->vmacache_seqnum != curr->vmacache.seqnum) {
  45. /*
  46. * First attempt will always be invalid, initialize
  47. * the new cache for this task here.
  48. */
  49. curr->vmacache.seqnum = mm->vmacache_seqnum;
  50. vmacache_flush(curr);
  51. return false;
  52. }
  53. return true;
  54. }
  55. struct vm_area_struct *vmacache_find(struct mm_struct *mm, unsigned long addr)
  56. {
  57. int idx = VMACACHE_HASH(addr);
  58. int i;
  59. count_vm_vmacache_event(VMACACHE_FIND_CALLS);
  60. if (!vmacache_valid(mm))
  61. return NULL;
  62. for (i = 0; i < VMACACHE_SIZE; i++) {
  63. struct vm_area_struct *vma = current->vmacache.vmas[idx];
  64. if (vma) {
  65. #ifdef CONFIG_DEBUG_VM_VMACACHE
  66. if (WARN_ON_ONCE(vma->vm_mm != mm))
  67. break;
  68. #endif
  69. if (vma->vm_start <= addr && vma->vm_end > addr) {
  70. count_vm_vmacache_event(VMACACHE_FIND_HITS);
  71. return vma;
  72. }
  73. }
  74. if (++idx == VMACACHE_SIZE)
  75. idx = 0;
  76. }
  77. return NULL;
  78. }
  79. #ifndef CONFIG_MMU
  80. struct vm_area_struct *vmacache_find_exact(struct mm_struct *mm,
  81. unsigned long start,
  82. unsigned long end)
  83. {
  84. int idx = VMACACHE_HASH(start);
  85. int i;
  86. count_vm_vmacache_event(VMACACHE_FIND_CALLS);
  87. if (!vmacache_valid(mm))
  88. return NULL;
  89. for (i = 0; i < VMACACHE_SIZE; i++) {
  90. struct vm_area_struct *vma = current->vmacache.vmas[idx];
  91. if (vma && vma->vm_start == start && vma->vm_end == end) {
  92. count_vm_vmacache_event(VMACACHE_FIND_HITS);
  93. return vma;
  94. }
  95. if (++idx == VMACACHE_SIZE)
  96. idx = 0;
  97. }
  98. return NULL;
  99. }
  100. #endif