mm.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. * This has to be called after a get_task_mm()/mmget_not_zero()
  48. * followed by taking the mmap_sem for writing before modifying the
  49. * vmas or anything the coredump pretends not to change from under it.
  50. *
  51. * NOTE: find_extend_vma() called from GUP context is the only place
  52. * that can modify the "mm" (notably the vm_start/end) under mmap_sem
  53. * for reading and outside the context of the process, so it is also
  54. * the only case that holds the mmap_sem for reading that must call
  55. * this function. Generally if the mmap_sem is hold for reading
  56. * there's no need of this check after get_task_mm()/mmget_not_zero().
  57. *
  58. * This function can be obsoleted and the check can be removed, after
  59. * the coredump code will hold the mmap_sem for writing before
  60. * invoking the ->core_dump methods.
  61. */
  62. static inline bool mmget_still_valid(struct mm_struct *mm)
  63. {
  64. return likely(!mm->core_state);
  65. }
  66. /**
  67. * mmget() - Pin the address space associated with a &struct mm_struct.
  68. * @mm: The address space to pin.
  69. *
  70. * Make sure that the address space of the given &struct mm_struct doesn't
  71. * go away. This does not protect against parts of the address space being
  72. * modified or freed, however.
  73. *
  74. * Never use this function to pin this address space for an
  75. * unbounded/indefinite amount of time.
  76. *
  77. * Use mmput() to release the reference acquired by mmget().
  78. *
  79. * See also <Documentation/vm/active_mm.rst> for an in-depth explanation
  80. * of &mm_struct.mm_count vs &mm_struct.mm_users.
  81. */
  82. static inline void mmget(struct mm_struct *mm)
  83. {
  84. atomic_inc(&mm->mm_users);
  85. }
  86. static inline bool mmget_not_zero(struct mm_struct *mm)
  87. {
  88. return atomic_inc_not_zero(&mm->mm_users);
  89. }
  90. /* mmput gets rid of the mappings and all user-space */
  91. extern void mmput(struct mm_struct *);
  92. #ifdef CONFIG_MMU
  93. /* same as above but performs the slow path from the async context. Can
  94. * be called from the atomic context as well
  95. */
  96. void mmput_async(struct mm_struct *);
  97. #endif
  98. /* Grab a reference to a task's mm, if it is not already going away */
  99. extern struct mm_struct *get_task_mm(struct task_struct *task);
  100. /*
  101. * Grab a reference to a task's mm, if it is not already going away
  102. * and ptrace_may_access with the mode parameter passed to it
  103. * succeeds.
  104. */
  105. extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode);
  106. /* Remove the current tasks stale references to the old mm_struct */
  107. extern void mm_release(struct task_struct *, struct mm_struct *);
  108. #ifdef CONFIG_MEMCG
  109. extern void mm_update_next_owner(struct mm_struct *mm);
  110. #else
  111. static inline void mm_update_next_owner(struct mm_struct *mm)
  112. {
  113. }
  114. #endif /* CONFIG_MEMCG */
  115. #ifdef CONFIG_MMU
  116. extern void arch_pick_mmap_layout(struct mm_struct *mm,
  117. struct rlimit *rlim_stack);
  118. extern unsigned long
  119. arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
  120. unsigned long, unsigned long);
  121. extern unsigned long
  122. arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
  123. unsigned long len, unsigned long pgoff,
  124. unsigned long flags);
  125. #else
  126. static inline void arch_pick_mmap_layout(struct mm_struct *mm,
  127. struct rlimit *rlim_stack) {}
  128. #endif
  129. static inline bool in_vfork(struct task_struct *tsk)
  130. {
  131. bool ret;
  132. /*
  133. * need RCU to access ->real_parent if CLONE_VM was used along with
  134. * CLONE_PARENT.
  135. *
  136. * We check real_parent->mm == tsk->mm because CLONE_VFORK does not
  137. * imply CLONE_VM
  138. *
  139. * CLONE_VFORK can be used with CLONE_PARENT/CLONE_THREAD and thus
  140. * ->real_parent is not necessarily the task doing vfork(), so in
  141. * theory we can't rely on task_lock() if we want to dereference it.
  142. *
  143. * And in this case we can't trust the real_parent->mm == tsk->mm
  144. * check, it can be false negative. But we do not care, if init or
  145. * another oom-unkillable task does this it should blame itself.
  146. */
  147. rcu_read_lock();
  148. ret = tsk->vfork_done && tsk->real_parent->mm == tsk->mm;
  149. rcu_read_unlock();
  150. return ret;
  151. }
  152. /*
  153. * Applies per-task gfp context to the given allocation flags.
  154. * PF_MEMALLOC_NOIO implies GFP_NOIO
  155. * PF_MEMALLOC_NOFS implies GFP_NOFS
  156. */
  157. static inline gfp_t current_gfp_context(gfp_t flags)
  158. {
  159. /*
  160. * NOIO implies both NOIO and NOFS and it is a weaker context
  161. * so always make sure it makes precendence
  162. */
  163. if (unlikely(current->flags & PF_MEMALLOC_NOIO))
  164. flags &= ~(__GFP_IO | __GFP_FS);
  165. else if (unlikely(current->flags & PF_MEMALLOC_NOFS))
  166. flags &= ~__GFP_FS;
  167. return flags;
  168. }
  169. #ifdef CONFIG_LOCKDEP
  170. extern void __fs_reclaim_acquire(void);
  171. extern void __fs_reclaim_release(void);
  172. extern void fs_reclaim_acquire(gfp_t gfp_mask);
  173. extern void fs_reclaim_release(gfp_t gfp_mask);
  174. #else
  175. static inline void __fs_reclaim_acquire(void) { }
  176. static inline void __fs_reclaim_release(void) { }
  177. static inline void fs_reclaim_acquire(gfp_t gfp_mask) { }
  178. static inline void fs_reclaim_release(gfp_t gfp_mask) { }
  179. #endif
  180. /**
  181. * memalloc_noio_save - Marks implicit GFP_NOIO allocation scope.
  182. *
  183. * This functions marks the beginning of the GFP_NOIO allocation scope.
  184. * All further allocations will implicitly drop __GFP_IO flag and so
  185. * they are safe for the IO critical section from the allocation recursion
  186. * point of view. Use memalloc_noio_restore to end the scope with flags
  187. * returned by this function.
  188. *
  189. * This function is safe to be used from any context.
  190. */
  191. static inline unsigned int memalloc_noio_save(void)
  192. {
  193. unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
  194. current->flags |= PF_MEMALLOC_NOIO;
  195. return flags;
  196. }
  197. /**
  198. * memalloc_noio_restore - Ends the implicit GFP_NOIO scope.
  199. * @flags: Flags to restore.
  200. *
  201. * Ends the implicit GFP_NOIO scope started by memalloc_noio_save function.
  202. * Always make sure that that the given flags is the return value from the
  203. * pairing memalloc_noio_save call.
  204. */
  205. static inline void memalloc_noio_restore(unsigned int flags)
  206. {
  207. current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
  208. }
  209. /**
  210. * memalloc_nofs_save - Marks implicit GFP_NOFS allocation scope.
  211. *
  212. * This functions marks the beginning of the GFP_NOFS allocation scope.
  213. * All further allocations will implicitly drop __GFP_FS flag and so
  214. * they are safe for the FS critical section from the allocation recursion
  215. * point of view. Use memalloc_nofs_restore to end the scope with flags
  216. * returned by this function.
  217. *
  218. * This function is safe to be used from any context.
  219. */
  220. static inline unsigned int memalloc_nofs_save(void)
  221. {
  222. unsigned int flags = current->flags & PF_MEMALLOC_NOFS;
  223. current->flags |= PF_MEMALLOC_NOFS;
  224. return flags;
  225. }
  226. /**
  227. * memalloc_nofs_restore - Ends the implicit GFP_NOFS scope.
  228. * @flags: Flags to restore.
  229. *
  230. * Ends the implicit GFP_NOFS scope started by memalloc_nofs_save function.
  231. * Always make sure that that the given flags is the return value from the
  232. * pairing memalloc_nofs_save call.
  233. */
  234. static inline void memalloc_nofs_restore(unsigned int flags)
  235. {
  236. current->flags = (current->flags & ~PF_MEMALLOC_NOFS) | flags;
  237. }
  238. static inline unsigned int memalloc_noreclaim_save(void)
  239. {
  240. unsigned int flags = current->flags & PF_MEMALLOC;
  241. current->flags |= PF_MEMALLOC;
  242. return flags;
  243. }
  244. static inline void memalloc_noreclaim_restore(unsigned int flags)
  245. {
  246. current->flags = (current->flags & ~PF_MEMALLOC) | flags;
  247. }
  248. #ifdef CONFIG_MEMCG
  249. /**
  250. * memalloc_use_memcg - Starts the remote memcg charging scope.
  251. * @memcg: memcg to charge.
  252. *
  253. * This function marks the beginning of the remote memcg charging scope. All the
  254. * __GFP_ACCOUNT allocations till the end of the scope will be charged to the
  255. * given memcg.
  256. *
  257. * NOTE: This function is not nesting safe.
  258. */
  259. static inline void memalloc_use_memcg(struct mem_cgroup *memcg)
  260. {
  261. WARN_ON_ONCE(current->active_memcg);
  262. current->active_memcg = memcg;
  263. }
  264. /**
  265. * memalloc_unuse_memcg - Ends the remote memcg charging scope.
  266. *
  267. * This function marks the end of the remote memcg charging scope started by
  268. * memalloc_use_memcg().
  269. */
  270. static inline void memalloc_unuse_memcg(void)
  271. {
  272. current->active_memcg = NULL;
  273. }
  274. #else
  275. static inline void memalloc_use_memcg(struct mem_cgroup *memcg)
  276. {
  277. }
  278. static inline void memalloc_unuse_memcg(void)
  279. {
  280. }
  281. #endif
  282. #ifdef CONFIG_MEMBARRIER
  283. enum {
  284. MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY = (1U << 0),
  285. MEMBARRIER_STATE_PRIVATE_EXPEDITED = (1U << 1),
  286. MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY = (1U << 2),
  287. MEMBARRIER_STATE_GLOBAL_EXPEDITED = (1U << 3),
  288. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY = (1U << 4),
  289. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE = (1U << 5),
  290. };
  291. enum {
  292. MEMBARRIER_FLAG_SYNC_CORE = (1U << 0),
  293. };
  294. #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
  295. #include <asm/membarrier.h>
  296. #endif
  297. static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
  298. {
  299. if (likely(!(atomic_read(&mm->membarrier_state) &
  300. MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE)))
  301. return;
  302. sync_core_before_usermode();
  303. }
  304. static inline void membarrier_execve(struct task_struct *t)
  305. {
  306. atomic_set(&t->mm->membarrier_state, 0);
  307. }
  308. #else
  309. #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
  310. static inline void membarrier_arch_switch_mm(struct mm_struct *prev,
  311. struct mm_struct *next,
  312. struct task_struct *tsk)
  313. {
  314. }
  315. #endif
  316. static inline void membarrier_execve(struct task_struct *t)
  317. {
  318. }
  319. static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
  320. {
  321. }
  322. #endif
  323. #endif /* _LINUX_SCHED_MM_H */