mmu_context.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Common implementation of switch_mm_irqs_off
  4. *
  5. * Copyright IBM Corp. 2017
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/cpu.h>
  9. #include <linux/sched/mm.h>
  10. #include <asm/mmu_context.h>
  11. #include <asm/pgalloc.h>
  12. #if defined(CONFIG_PPC32)
  13. static inline void switch_mm_pgdir(struct task_struct *tsk,
  14. struct mm_struct *mm)
  15. {
  16. /* 32-bit keeps track of the current PGDIR in the thread struct */
  17. tsk->thread.pgdir = mm->pgd;
  18. #ifdef CONFIG_PPC_BOOK3S_32
  19. tsk->thread.sr0 = mm->context.sr0;
  20. #endif
  21. #if defined(CONFIG_BOOKE) && defined(CONFIG_PPC_KUAP)
  22. tsk->thread.pid = mm->context.id;
  23. #endif
  24. }
  25. #elif defined(CONFIG_PPC_BOOK3E_64)
  26. static inline void switch_mm_pgdir(struct task_struct *tsk,
  27. struct mm_struct *mm)
  28. {
  29. /* 64-bit Book3E keeps track of current PGD in the PACA */
  30. get_paca()->pgd = mm->pgd;
  31. #ifdef CONFIG_PPC_KUAP
  32. tsk->thread.pid = mm->context.id;
  33. #endif
  34. }
  35. #else
  36. static inline void switch_mm_pgdir(struct task_struct *tsk,
  37. struct mm_struct *mm) { }
  38. #endif
  39. void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
  40. struct task_struct *tsk)
  41. {
  42. int cpu = smp_processor_id();
  43. bool new_on_cpu = false;
  44. /* Mark this context has been used on the new CPU */
  45. if (!cpumask_test_cpu(cpu, mm_cpumask(next))) {
  46. VM_WARN_ON_ONCE(next == &init_mm);
  47. cpumask_set_cpu(cpu, mm_cpumask(next));
  48. inc_mm_active_cpus(next);
  49. /*
  50. * This full barrier orders the store to the cpumask above vs
  51. * a subsequent load which allows this CPU/MMU to begin loading
  52. * translations for 'next' from page table PTEs into the TLB.
  53. *
  54. * When using the radix MMU, that operation is the load of the
  55. * MMU context id, which is then moved to SPRN_PID.
  56. *
  57. * For the hash MMU it is either the first load from slb_cache
  58. * in switch_slb() to preload the SLBs, or the load of
  59. * get_user_context which loads the context for the VSID hash
  60. * to insert a new SLB, in the SLB fault handler.
  61. *
  62. * On the other side, the barrier is in mm/tlb-radix.c for
  63. * radix which orders earlier stores to clear the PTEs before
  64. * the load of mm_cpumask to check which CPU TLBs should be
  65. * flushed. For hash, pte_xchg to clear the PTE includes the
  66. * barrier.
  67. *
  68. * This full barrier is also needed by membarrier when
  69. * switching between processes after store to rq->curr, before
  70. * user-space memory accesses.
  71. */
  72. smp_mb();
  73. new_on_cpu = true;
  74. }
  75. /* Some subarchs need to track the PGD elsewhere */
  76. switch_mm_pgdir(tsk, next);
  77. /* Nothing else to do if we aren't actually switching */
  78. if (prev == next)
  79. return;
  80. /*
  81. * We must stop all altivec streams before changing the HW
  82. * context
  83. */
  84. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  85. asm volatile (PPC_DSSALL);
  86. if (!new_on_cpu)
  87. membarrier_arch_switch_mm(prev, next, tsk);
  88. /*
  89. * The actual HW switching method differs between the various
  90. * sub architectures. Out of line for now
  91. */
  92. switch_mmu_context(prev, next, tsk);
  93. VM_WARN_ON_ONCE(!cpumask_test_cpu(cpu, mm_cpumask(prev)));
  94. }
  95. #ifndef CONFIG_PPC_BOOK3S_64
  96. void arch_exit_mmap(struct mm_struct *mm)
  97. {
  98. void *frag = pte_frag_get(&mm->context);
  99. if (frag)
  100. pte_frag_destroy(frag);
  101. }
  102. #endif