mm.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.rst> 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.rst> 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. struct rlimit *rlim_stack);
  98. extern unsigned long
  99. arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
  100. unsigned long, unsigned long);
  101. extern unsigned long
  102. arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
  103. unsigned long len, unsigned long pgoff,
  104. unsigned long flags);
  105. #else
  106. static inline void arch_pick_mmap_layout(struct mm_struct *mm,
  107. struct rlimit *rlim_stack) {}
  108. #endif
  109. static inline bool in_vfork(struct task_struct *tsk)
  110. {
  111. bool ret;
  112. /*
  113. * need RCU to access ->real_parent if CLONE_VM was used along with
  114. * CLONE_PARENT.
  115. *
  116. * We check real_parent->mm == tsk->mm because CLONE_VFORK does not
  117. * imply CLONE_VM
  118. *
  119. * CLONE_VFORK can be used with CLONE_PARENT/CLONE_THREAD and thus
  120. * ->real_parent is not necessarily the task doing vfork(), so in
  121. * theory we can't rely on task_lock() if we want to dereference it.
  122. *
  123. * And in this case we can't trust the real_parent->mm == tsk->mm
  124. * check, it can be false negative. But we do not care, if init or
  125. * another oom-unkillable task does this it should blame itself.
  126. */
  127. rcu_read_lock();
  128. ret = tsk->vfork_done && tsk->real_parent->mm == tsk->mm;
  129. rcu_read_unlock();
  130. return ret;
  131. }
  132. /*
  133. * Applies per-task gfp context to the given allocation flags.
  134. * PF_MEMALLOC_NOIO implies GFP_NOIO
  135. * PF_MEMALLOC_NOFS implies GFP_NOFS
  136. */
  137. static inline gfp_t current_gfp_context(gfp_t flags)
  138. {
  139. /*
  140. * NOIO implies both NOIO and NOFS and it is a weaker context
  141. * so always make sure it makes precendence
  142. */
  143. if (unlikely(current->flags & PF_MEMALLOC_NOIO))
  144. flags &= ~(__GFP_IO | __GFP_FS);
  145. else if (unlikely(current->flags & PF_MEMALLOC_NOFS))
  146. flags &= ~__GFP_FS;
  147. return flags;
  148. }
  149. #ifdef CONFIG_LOCKDEP
  150. extern void __fs_reclaim_acquire(void);
  151. extern void __fs_reclaim_release(void);
  152. extern void fs_reclaim_acquire(gfp_t gfp_mask);
  153. extern void fs_reclaim_release(gfp_t gfp_mask);
  154. #else
  155. static inline void __fs_reclaim_acquire(void) { }
  156. static inline void __fs_reclaim_release(void) { }
  157. static inline void fs_reclaim_acquire(gfp_t gfp_mask) { }
  158. static inline void fs_reclaim_release(gfp_t gfp_mask) { }
  159. #endif
  160. /**
  161. * memalloc_noio_save - Marks implicit GFP_NOIO allocation scope.
  162. *
  163. * This functions marks the beginning of the GFP_NOIO allocation scope.
  164. * All further allocations will implicitly drop __GFP_IO flag and so
  165. * they are safe for the IO critical section from the allocation recursion
  166. * point of view. Use memalloc_noio_restore to end the scope with flags
  167. * returned by this function.
  168. *
  169. * This function is safe to be used from any context.
  170. */
  171. static inline unsigned int memalloc_noio_save(void)
  172. {
  173. unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
  174. current->flags |= PF_MEMALLOC_NOIO;
  175. return flags;
  176. }
  177. /**
  178. * memalloc_noio_restore - Ends the implicit GFP_NOIO scope.
  179. * @flags: Flags to restore.
  180. *
  181. * Ends the implicit GFP_NOIO scope started by memalloc_noio_save function.
  182. * Always make sure that that the given flags is the return value from the
  183. * pairing memalloc_noio_save call.
  184. */
  185. static inline void memalloc_noio_restore(unsigned int flags)
  186. {
  187. current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
  188. }
  189. /**
  190. * memalloc_nofs_save - Marks implicit GFP_NOFS allocation scope.
  191. *
  192. * This functions marks the beginning of the GFP_NOFS allocation scope.
  193. * All further allocations will implicitly drop __GFP_FS flag and so
  194. * they are safe for the FS critical section from the allocation recursion
  195. * point of view. Use memalloc_nofs_restore to end the scope with flags
  196. * returned by this function.
  197. *
  198. * This function is safe to be used from any context.
  199. */
  200. static inline unsigned int memalloc_nofs_save(void)
  201. {
  202. unsigned int flags = current->flags & PF_MEMALLOC_NOFS;
  203. current->flags |= PF_MEMALLOC_NOFS;
  204. return flags;
  205. }
  206. /**
  207. * memalloc_nofs_restore - Ends the implicit GFP_NOFS scope.
  208. * @flags: Flags to restore.
  209. *
  210. * Ends the implicit GFP_NOFS scope started by memalloc_nofs_save function.
  211. * Always make sure that that the given flags is the return value from the
  212. * pairing memalloc_nofs_save call.
  213. */
  214. static inline void memalloc_nofs_restore(unsigned int flags)
  215. {
  216. current->flags = (current->flags & ~PF_MEMALLOC_NOFS) | flags;
  217. }
  218. static inline unsigned int memalloc_noreclaim_save(void)
  219. {
  220. unsigned int flags = current->flags & PF_MEMALLOC;
  221. current->flags |= PF_MEMALLOC;
  222. return flags;
  223. }
  224. static inline void memalloc_noreclaim_restore(unsigned int flags)
  225. {
  226. current->flags = (current->flags & ~PF_MEMALLOC) | flags;
  227. }
  228. #ifdef CONFIG_MEMBARRIER
  229. enum {
  230. MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY = (1U << 0),
  231. MEMBARRIER_STATE_PRIVATE_EXPEDITED = (1U << 1),
  232. MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY = (1U << 2),
  233. MEMBARRIER_STATE_GLOBAL_EXPEDITED = (1U << 3),
  234. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY = (1U << 4),
  235. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE = (1U << 5),
  236. };
  237. enum {
  238. MEMBARRIER_FLAG_SYNC_CORE = (1U << 0),
  239. };
  240. #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
  241. #include <asm/membarrier.h>
  242. #endif
  243. static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
  244. {
  245. if (likely(!(atomic_read(&mm->membarrier_state) &
  246. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE)))
  247. return;
  248. sync_core_before_usermode();
  249. }
  250. static inline void membarrier_execve(struct task_struct *t)
  251. {
  252. atomic_set(&t->mm->membarrier_state, 0);
  253. }
  254. #else
  255. #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
  256. static inline void membarrier_arch_switch_mm(struct mm_struct *prev,
  257. struct mm_struct *next,
  258. struct task_struct *tsk)
  259. {
  260. }
  261. #endif
  262. static inline void membarrier_execve(struct task_struct *t)
  263. {
  264. }
  265. static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
  266. {
  267. }
  268. #endif
  269. #endif /* _LINUX_SCHED_MM_H */