vmacache.c 2.1 KB

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