mm.h 7.4 KB

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