mm.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_SCHED_MM_H
  3. #define _LINUX_SCHED_MM_H
  4. #include <linux/kernel.h>
  5. #include <linux/atomic.h>
  6. #include <linux/sched.h>
  7. #include <linux/mm_types.h>
  8. #include <linux/gfp.h>
  9. #include <linux/sync_core.h>
  10. /*
  11. * Routines for handling mm_structs
  12. */
  13. extern struct mm_struct *mm_alloc(void);
  14. /**
  15. * mmgrab() - Pin a &struct mm_struct.
  16. * @mm: The &struct mm_struct to pin.
  17. *
  18. * Make sure that @mm will not get freed even after the owning task
  19. * exits. This doesn't guarantee that the associated address space
  20. * will still exist later on and mmget_not_zero() has to be used before
  21. * accessing it.
  22. *
  23. * This is a preferred way to to pin @mm for a longer/unbounded amount
  24. * of time.
  25. *
  26. * Use mmdrop() to release the reference acquired by mmgrab().
  27. *
  28. * See also <Documentation/vm/active_mm.txt> for an in-depth explanation
  29. * of &mm_struct.mm_count vs &mm_struct.mm_users.
  30. */
  31. static inline void mmgrab(struct mm_struct *mm)
  32. {
  33. atomic_inc(&mm->mm_count);
  34. }
  35. extern void __mmdrop(struct mm_struct *mm);
  36. static inline void mmdrop(struct mm_struct *mm)
  37. {
  38. /*
  39. * The implicit full barrier implied by atomic_dec_and_test() is
  40. * required by the membarrier system call before returning to
  41. * user-space, after storing to rq->curr.
  42. */
  43. if (unlikely(atomic_dec_and_test(&mm->mm_count)))
  44. __mmdrop(mm);
  45. }
  46. /**
  47. * mmget() - Pin the address space associated with a &struct mm_struct.
  48. * @mm: The address space to pin.
  49. *
  50. * Make sure that the address space of the given &struct mm_struct doesn't
  51. * go away. This does not protect against parts of the address space being
  52. * modified or freed, however.
  53. *
  54. * Never use this function to pin this address space for an
  55. * unbounded/indefinite amount of time.
  56. *
  57. * Use mmput() to release the reference acquired by mmget().
  58. *
  59. * See also <Documentation/vm/active_mm.txt> for an in-depth explanation
  60. * of &mm_struct.mm_count vs &mm_struct.mm_users.
  61. */
  62. static inline void mmget(struct mm_struct *mm)
  63. {
  64. atomic_inc(&mm->mm_users);
  65. }
  66. static inline bool mmget_not_zero(struct mm_struct *mm)
  67. {
  68. return atomic_inc_not_zero(&mm->mm_users);
  69. }
  70. /* mmput gets rid of the mappings and all user-space */
  71. extern void mmput(struct mm_struct *);
  72. #ifdef CONFIG_MMU
  73. /* same as above but performs the slow path from the async context. Can
  74. * be called from the atomic context as well
  75. */
  76. void mmput_async(struct mm_struct *);
  77. #endif
  78. /* Grab a reference to a task's mm, if it is not already going away */
  79. extern struct mm_struct *get_task_mm(struct task_struct *task);
  80. /*
  81. * Grab a reference to a task's mm, if it is not already going away
  82. * and ptrace_may_access with the mode parameter passed to it
  83. * succeeds.
  84. */
  85. extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode);
  86. /* Remove the current tasks stale references to the old mm_struct */
  87. extern void mm_release(struct task_struct *, struct mm_struct *);
  88. #ifdef CONFIG_MEMCG
  89. extern void mm_update_next_owner(struct mm_struct *mm);
  90. #else
  91. static inline void mm_update_next_owner(struct mm_struct *mm)
  92. {
  93. }
  94. #endif /* CONFIG_MEMCG */
  95. #ifdef CONFIG_MMU
  96. extern void arch_pick_mmap_layout(struct mm_struct *mm);
  97. extern unsigned long
  98. arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
  99. unsigned long, unsigned long);
  100. extern unsigned long
  101. arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
  102. unsigned long len, unsigned long pgoff,
  103. unsigned long flags);
  104. #else
  105. static inline void arch_pick_mmap_layout(struct mm_struct *mm) {}
  106. #endif
  107. static inline bool in_vfork(struct task_struct *tsk)
  108. {
  109. bool ret;
  110. /*
  111. * need RCU to access ->real_parent if CLONE_VM was used along with
  112. * CLONE_PARENT.
  113. *
  114. * We check real_parent->mm == tsk->mm because CLONE_VFORK does not
  115. * imply CLONE_VM
  116. *
  117. * CLONE_VFORK can be used with CLONE_PARENT/CLONE_THREAD and thus
  118. * ->real_parent is not necessarily the task doing vfork(), so in
  119. * theory we can't rely on task_lock() if we want to dereference it.
  120. *
  121. * And in this case we can't trust the real_parent->mm == tsk->mm
  122. * check, it can be false negative. But we do not care, if init or
  123. * another oom-unkillable task does this it should blame itself.
  124. */
  125. rcu_read_lock();
  126. ret = tsk->vfork_done && tsk->real_parent->mm == tsk->mm;
  127. rcu_read_unlock();
  128. return ret;
  129. }
  130. /*
  131. * Applies per-task gfp context to the given allocation flags.
  132. * PF_MEMALLOC_NOIO implies GFP_NOIO
  133. * PF_MEMALLOC_NOFS implies GFP_NOFS
  134. */
  135. static inline gfp_t current_gfp_context(gfp_t flags)
  136. {
  137. /*
  138. * NOIO implies both NOIO and NOFS and it is a weaker context
  139. * so always make sure it makes precendence
  140. */
  141. if (unlikely(current->flags & PF_MEMALLOC_NOIO))
  142. flags &= ~(__GFP_IO | __GFP_FS);
  143. else if (unlikely(current->flags & PF_MEMALLOC_NOFS))
  144. flags &= ~__GFP_FS;
  145. return flags;
  146. }
  147. #ifdef CONFIG_LOCKDEP
  148. extern void fs_reclaim_acquire(gfp_t gfp_mask);
  149. extern void fs_reclaim_release(gfp_t gfp_mask);
  150. #else
  151. static inline void fs_reclaim_acquire(gfp_t gfp_mask) { }
  152. static inline void fs_reclaim_release(gfp_t gfp_mask) { }
  153. #endif
  154. static inline unsigned int memalloc_noio_save(void)
  155. {
  156. unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
  157. current->flags |= PF_MEMALLOC_NOIO;
  158. return flags;
  159. }
  160. static inline void memalloc_noio_restore(unsigned int flags)
  161. {
  162. current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
  163. }
  164. static inline unsigned int memalloc_nofs_save(void)
  165. {
  166. unsigned int flags = current->flags & PF_MEMALLOC_NOFS;
  167. current->flags |= PF_MEMALLOC_NOFS;
  168. return flags;
  169. }
  170. static inline void memalloc_nofs_restore(unsigned int flags)
  171. {
  172. current->flags = (current->flags & ~PF_MEMALLOC_NOFS) | flags;
  173. }
  174. static inline unsigned int memalloc_noreclaim_save(void)
  175. {
  176. unsigned int flags = current->flags & PF_MEMALLOC;
  177. current->flags |= PF_MEMALLOC;
  178. return flags;
  179. }
  180. static inline void memalloc_noreclaim_restore(unsigned int flags)
  181. {
  182. current->flags = (current->flags & ~PF_MEMALLOC) | flags;
  183. }
  184. #ifdef CONFIG_MEMBARRIER
  185. enum {
  186. MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY = (1U << 0),
  187. MEMBARRIER_STATE_PRIVATE_EXPEDITED = (1U << 1),
  188. MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY = (1U << 2),
  189. MEMBARRIER_STATE_GLOBAL_EXPEDITED = (1U << 3),
  190. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY = (1U << 4),
  191. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE = (1U << 5),
  192. };
  193. enum {
  194. MEMBARRIER_FLAG_SYNC_CORE = (1U << 0),
  195. };
  196. #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
  197. #include <asm/membarrier.h>
  198. #endif
  199. static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
  200. {
  201. if (likely(!(atomic_read(&mm->membarrier_state) &
  202. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE)))
  203. return;
  204. sync_core_before_usermode();
  205. }
  206. static inline void membarrier_execve(struct task_struct *t)
  207. {
  208. atomic_set(&t->mm->membarrier_state, 0);
  209. }
  210. #else
  211. #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
  212. static inline void membarrier_arch_switch_mm(struct mm_struct *prev,
  213. struct mm_struct *next,
  214. struct task_struct *tsk)
  215. {
  216. }
  217. #endif
  218. static inline void membarrier_execve(struct task_struct *t)
  219. {
  220. }
  221. static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
  222. {
  223. }
  224. #endif
  225. #endif /* _LINUX_SCHED_MM_H */