1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SCHED_MM_H
3 #define _LINUX_SCHED_MM_H
5 #include <linux/kernel.h>
6 #include <linux/atomic.h>
7 #include <linux/sched.h>
8 #include <linux/mm_types.h>
10 #include <linux/sync_core.h>
11 #include <linux/ioasid.h>
14 * Routines for handling mm_structs
16 extern struct mm_struct *mm_alloc(void);
19 * mmgrab() - Pin a &struct mm_struct.
20 * @mm: The &struct mm_struct to pin.
22 * Make sure that @mm will not get freed even after the owning task
23 * exits. This doesn't guarantee that the associated address space
24 * will still exist later on and mmget_not_zero() has to be used before
27 * This is a preferred way to pin @mm for a longer/unbounded amount
30 * Use mmdrop() to release the reference acquired by mmgrab().
32 * See also <Documentation/mm/active_mm.rst> for an in-depth explanation
33 * of &mm_struct.mm_count vs &mm_struct.mm_users.
35 static inline void mmgrab(struct mm_struct *mm)
37 atomic_inc(&mm->mm_count);
40 static inline void smp_mb__after_mmgrab(void)
42 smp_mb__after_atomic();
45 extern void __mmdrop(struct mm_struct *mm);
47 static inline void mmdrop(struct mm_struct *mm)
50 * The implicit full barrier implied by atomic_dec_and_test() is
51 * required by the membarrier system call before returning to
52 * user-space, after storing to rq->curr.
54 if (unlikely(atomic_dec_and_test(&mm->mm_count)))
58 #ifdef CONFIG_PREEMPT_RT
60 * RCU callback for delayed mm drop. Not strictly RCU, but call_rcu() is
61 * by far the least expensive way to do that.
63 static inline void __mmdrop_delayed(struct rcu_head *rhp)
65 struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
71 * Invoked from finish_task_switch(). Delegates the heavy lifting on RT
74 static inline void mmdrop_sched(struct mm_struct *mm)
76 /* Provides a full memory barrier. See mmdrop() */
77 if (atomic_dec_and_test(&mm->mm_count))
78 call_rcu(&mm->delayed_drop, __mmdrop_delayed);
81 static inline void mmdrop_sched(struct mm_struct *mm)
88 * mmget() - Pin the address space associated with a &struct mm_struct.
89 * @mm: The address space to pin.
91 * Make sure that the address space of the given &struct mm_struct doesn't
92 * go away. This does not protect against parts of the address space being
93 * modified or freed, however.
95 * Never use this function to pin this address space for an
96 * unbounded/indefinite amount of time.
98 * Use mmput() to release the reference acquired by mmget().
100 * See also <Documentation/mm/active_mm.rst> for an in-depth explanation
101 * of &mm_struct.mm_count vs &mm_struct.mm_users.
103 static inline void mmget(struct mm_struct *mm)
105 atomic_inc(&mm->mm_users);
108 static inline bool mmget_not_zero(struct mm_struct *mm)
110 return atomic_inc_not_zero(&mm->mm_users);
113 /* mmput gets rid of the mappings and all user-space */
114 extern void mmput(struct mm_struct *);
116 /* same as above but performs the slow path from the async context. Can
117 * be called from the atomic context as well
119 void mmput_async(struct mm_struct *);
122 /* Grab a reference to a task's mm, if it is not already going away */
123 extern struct mm_struct *get_task_mm(struct task_struct *task);
125 * Grab a reference to a task's mm, if it is not already going away
126 * and ptrace_may_access with the mode parameter passed to it
129 extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode);
130 /* Remove the current tasks stale references to the old mm_struct on exit() */
131 extern void exit_mm_release(struct task_struct *, struct mm_struct *);
132 /* Remove the current tasks stale references to the old mm_struct on exec() */
133 extern void exec_mm_release(struct task_struct *, struct mm_struct *);
136 extern void mm_update_next_owner(struct mm_struct *mm);
138 static inline void mm_update_next_owner(struct mm_struct *mm)
141 #endif /* CONFIG_MEMCG */
144 #ifndef arch_get_mmap_end
145 #define arch_get_mmap_end(addr, len, flags) (TASK_SIZE)
148 #ifndef arch_get_mmap_base
149 #define arch_get_mmap_base(addr, base) (base)
152 extern void arch_pick_mmap_layout(struct mm_struct *mm,
153 struct rlimit *rlim_stack);
155 arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
156 unsigned long, unsigned long);
158 arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
159 unsigned long len, unsigned long pgoff,
160 unsigned long flags);
163 generic_get_unmapped_area(struct file *filp, unsigned long addr,
164 unsigned long len, unsigned long pgoff,
165 unsigned long flags);
167 generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
168 unsigned long len, unsigned long pgoff,
169 unsigned long flags);
171 static inline void arch_pick_mmap_layout(struct mm_struct *mm,
172 struct rlimit *rlim_stack) {}
175 static inline bool in_vfork(struct task_struct *tsk)
180 * need RCU to access ->real_parent if CLONE_VM was used along with
183 * We check real_parent->mm == tsk->mm because CLONE_VFORK does not
186 * CLONE_VFORK can be used with CLONE_PARENT/CLONE_THREAD and thus
187 * ->real_parent is not necessarily the task doing vfork(), so in
188 * theory we can't rely on task_lock() if we want to dereference it.
190 * And in this case we can't trust the real_parent->mm == tsk->mm
191 * check, it can be false negative. But we do not care, if init or
192 * another oom-unkillable task does this it should blame itself.
195 ret = tsk->vfork_done &&
196 rcu_dereference(tsk->real_parent)->mm == tsk->mm;
203 * Applies per-task gfp context to the given allocation flags.
204 * PF_MEMALLOC_NOIO implies GFP_NOIO
205 * PF_MEMALLOC_NOFS implies GFP_NOFS
206 * PF_MEMALLOC_PIN implies !GFP_MOVABLE
208 static inline gfp_t current_gfp_context(gfp_t flags)
210 unsigned int pflags = READ_ONCE(current->flags);
212 if (unlikely(pflags & (PF_MEMALLOC_NOIO | PF_MEMALLOC_NOFS | PF_MEMALLOC_PIN))) {
214 * NOIO implies both NOIO and NOFS and it is a weaker context
215 * so always make sure it makes precedence
217 if (pflags & PF_MEMALLOC_NOIO)
218 flags &= ~(__GFP_IO | __GFP_FS);
219 else if (pflags & PF_MEMALLOC_NOFS)
222 if (pflags & PF_MEMALLOC_PIN)
223 flags &= ~__GFP_MOVABLE;
228 #ifdef CONFIG_LOCKDEP
229 extern void __fs_reclaim_acquire(unsigned long ip);
230 extern void __fs_reclaim_release(unsigned long ip);
231 extern void fs_reclaim_acquire(gfp_t gfp_mask);
232 extern void fs_reclaim_release(gfp_t gfp_mask);
234 static inline void __fs_reclaim_acquire(unsigned long ip) { }
235 static inline void __fs_reclaim_release(unsigned long ip) { }
236 static inline void fs_reclaim_acquire(gfp_t gfp_mask) { }
237 static inline void fs_reclaim_release(gfp_t gfp_mask) { }
240 /* Any memory-allocation retry loop should use
241 * memalloc_retry_wait(), and pass the flags for the most
242 * constrained allocation attempt that might have failed.
243 * This provides useful documentation of where loops are,
244 * and a central place to fine tune the waiting as the MM
245 * implementation changes.
247 static inline void memalloc_retry_wait(gfp_t gfp_flags)
249 /* We use io_schedule_timeout because waiting for memory
250 * typically included waiting for dirty pages to be
251 * written out, which requires IO.
253 __set_current_state(TASK_UNINTERRUPTIBLE);
254 gfp_flags = current_gfp_context(gfp_flags);
255 if (gfpflags_allow_blocking(gfp_flags) &&
256 !(gfp_flags & __GFP_NORETRY))
257 /* Probably waited already, no need for much more */
258 io_schedule_timeout(1);
260 /* Probably didn't wait, and has now released a lock,
261 * so now is a good time to wait
263 io_schedule_timeout(HZ/50);
267 * might_alloc - Mark possible allocation sites
268 * @gfp_mask: gfp_t flags that would be used to allocate
270 * Similar to might_sleep() and other annotations, this can be used in functions
271 * that might allocate, but often don't. Compiles to nothing without
272 * CONFIG_LOCKDEP. Includes a conditional might_sleep() if @gfp allows blocking.
274 static inline void might_alloc(gfp_t gfp_mask)
276 fs_reclaim_acquire(gfp_mask);
277 fs_reclaim_release(gfp_mask);
279 might_sleep_if(gfpflags_allow_blocking(gfp_mask));
283 * memalloc_noio_save - Marks implicit GFP_NOIO allocation scope.
285 * This functions marks the beginning of the GFP_NOIO allocation scope.
286 * All further allocations will implicitly drop __GFP_IO flag and so
287 * they are safe for the IO critical section from the allocation recursion
288 * point of view. Use memalloc_noio_restore to end the scope with flags
289 * returned by this function.
291 * This function is safe to be used from any context.
293 static inline unsigned int memalloc_noio_save(void)
295 unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
296 current->flags |= PF_MEMALLOC_NOIO;
301 * memalloc_noio_restore - Ends the implicit GFP_NOIO scope.
302 * @flags: Flags to restore.
304 * Ends the implicit GFP_NOIO scope started by memalloc_noio_save function.
305 * Always make sure that the given flags is the return value from the
306 * pairing memalloc_noio_save call.
308 static inline void memalloc_noio_restore(unsigned int flags)
310 current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
314 * memalloc_nofs_save - Marks implicit GFP_NOFS allocation scope.
316 * This functions marks the beginning of the GFP_NOFS allocation scope.
317 * All further allocations will implicitly drop __GFP_FS flag and so
318 * they are safe for the FS critical section from the allocation recursion
319 * point of view. Use memalloc_nofs_restore to end the scope with flags
320 * returned by this function.
322 * This function is safe to be used from any context.
324 static inline unsigned int memalloc_nofs_save(void)
326 unsigned int flags = current->flags & PF_MEMALLOC_NOFS;
327 current->flags |= PF_MEMALLOC_NOFS;
332 * memalloc_nofs_restore - Ends the implicit GFP_NOFS scope.
333 * @flags: Flags to restore.
335 * Ends the implicit GFP_NOFS scope started by memalloc_nofs_save function.
336 * Always make sure that the given flags is the return value from the
337 * pairing memalloc_nofs_save call.
339 static inline void memalloc_nofs_restore(unsigned int flags)
341 current->flags = (current->flags & ~PF_MEMALLOC_NOFS) | flags;
344 static inline unsigned int memalloc_noreclaim_save(void)
346 unsigned int flags = current->flags & PF_MEMALLOC;
347 current->flags |= PF_MEMALLOC;
351 static inline void memalloc_noreclaim_restore(unsigned int flags)
353 current->flags = (current->flags & ~PF_MEMALLOC) | flags;
356 static inline unsigned int memalloc_pin_save(void)
358 unsigned int flags = current->flags & PF_MEMALLOC_PIN;
360 current->flags |= PF_MEMALLOC_PIN;
364 static inline void memalloc_pin_restore(unsigned int flags)
366 current->flags = (current->flags & ~PF_MEMALLOC_PIN) | flags;
370 DECLARE_PER_CPU(struct mem_cgroup *, int_active_memcg);
372 * set_active_memcg - Starts the remote memcg charging scope.
373 * @memcg: memcg to charge.
375 * This function marks the beginning of the remote memcg charging scope. All the
376 * __GFP_ACCOUNT allocations till the end of the scope will be charged to the
379 * NOTE: This function can nest. Users must save the return value and
380 * reset the previous value after their own charging scope is over.
382 static inline struct mem_cgroup *
383 set_active_memcg(struct mem_cgroup *memcg)
385 struct mem_cgroup *old;
388 old = this_cpu_read(int_active_memcg);
389 this_cpu_write(int_active_memcg, memcg);
391 old = current->active_memcg;
392 current->active_memcg = memcg;
398 static inline struct mem_cgroup *
399 set_active_memcg(struct mem_cgroup *memcg)
405 #ifdef CONFIG_MEMBARRIER
407 MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY = (1U << 0),
408 MEMBARRIER_STATE_PRIVATE_EXPEDITED = (1U << 1),
409 MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY = (1U << 2),
410 MEMBARRIER_STATE_GLOBAL_EXPEDITED = (1U << 3),
411 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY = (1U << 4),
412 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE = (1U << 5),
413 MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY = (1U << 6),
414 MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ = (1U << 7),
418 MEMBARRIER_FLAG_SYNC_CORE = (1U << 0),
419 MEMBARRIER_FLAG_RSEQ = (1U << 1),
422 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
423 #include <asm/membarrier.h>
426 static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
428 if (current->mm != mm)
430 if (likely(!(atomic_read(&mm->membarrier_state) &
431 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE)))
433 sync_core_before_usermode();
436 extern void membarrier_exec_mmap(struct mm_struct *mm);
438 extern void membarrier_update_current_mm(struct mm_struct *next_mm);
441 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
442 static inline void membarrier_arch_switch_mm(struct mm_struct *prev,
443 struct mm_struct *next,
444 struct task_struct *tsk)
448 static inline void membarrier_exec_mmap(struct mm_struct *mm)
451 static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
454 static inline void membarrier_update_current_mm(struct mm_struct *next_mm)
459 #ifdef CONFIG_IOMMU_SVA
460 static inline void mm_pasid_init(struct mm_struct *mm)
462 mm->pasid = INVALID_IOASID;
465 /* Associate a PASID with an mm_struct: */
466 static inline void mm_pasid_set(struct mm_struct *mm, u32 pasid)
471 static inline void mm_pasid_drop(struct mm_struct *mm)
473 if (pasid_valid(mm->pasid)) {
474 ioasid_free(mm->pasid);
475 mm->pasid = INVALID_IOASID;
479 static inline void mm_pasid_init(struct mm_struct *mm) {}
480 static inline void mm_pasid_set(struct mm_struct *mm, u32 pasid) {}
481 static inline void mm_pasid_drop(struct mm_struct *mm) {}
484 #endif /* _LINUX_SCHED_MM_H */