Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_SCHED_MM_H
0003 #define _LINUX_SCHED_MM_H
0004 
0005 #include <linux/kernel.h>
0006 #include <linux/atomic.h>
0007 #include <linux/sched.h>
0008 #include <linux/mm_types.h>
0009 #include <linux/gfp.h>
0010 #include <linux/sync_core.h>
0011 #include <linux/ioasid.h>
0012 
0013 /*
0014  * Routines for handling mm_structs
0015  */
0016 extern struct mm_struct *mm_alloc(void);
0017 
0018 /**
0019  * mmgrab() - Pin a &struct mm_struct.
0020  * @mm: The &struct mm_struct to pin.
0021  *
0022  * Make sure that @mm will not get freed even after the owning task
0023  * exits. This doesn't guarantee that the associated address space
0024  * will still exist later on and mmget_not_zero() has to be used before
0025  * accessing it.
0026  *
0027  * This is a preferred way to pin @mm for a longer/unbounded amount
0028  * of time.
0029  *
0030  * Use mmdrop() to release the reference acquired by mmgrab().
0031  *
0032  * See also <Documentation/mm/active_mm.rst> for an in-depth explanation
0033  * of &mm_struct.mm_count vs &mm_struct.mm_users.
0034  */
0035 static inline void mmgrab(struct mm_struct *mm)
0036 {
0037     atomic_inc(&mm->mm_count);
0038 }
0039 
0040 extern void __mmdrop(struct mm_struct *mm);
0041 
0042 static inline void mmdrop(struct mm_struct *mm)
0043 {
0044     /*
0045      * The implicit full barrier implied by atomic_dec_and_test() is
0046      * required by the membarrier system call before returning to
0047      * user-space, after storing to rq->curr.
0048      */
0049     if (unlikely(atomic_dec_and_test(&mm->mm_count)))
0050         __mmdrop(mm);
0051 }
0052 
0053 #ifdef CONFIG_PREEMPT_RT
0054 /*
0055  * RCU callback for delayed mm drop. Not strictly RCU, but call_rcu() is
0056  * by far the least expensive way to do that.
0057  */
0058 static inline void __mmdrop_delayed(struct rcu_head *rhp)
0059 {
0060     struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
0061 
0062     __mmdrop(mm);
0063 }
0064 
0065 /*
0066  * Invoked from finish_task_switch(). Delegates the heavy lifting on RT
0067  * kernels via RCU.
0068  */
0069 static inline void mmdrop_sched(struct mm_struct *mm)
0070 {
0071     /* Provides a full memory barrier. See mmdrop() */
0072     if (atomic_dec_and_test(&mm->mm_count))
0073         call_rcu(&mm->delayed_drop, __mmdrop_delayed);
0074 }
0075 #else
0076 static inline void mmdrop_sched(struct mm_struct *mm)
0077 {
0078     mmdrop(mm);
0079 }
0080 #endif
0081 
0082 /**
0083  * mmget() - Pin the address space associated with a &struct mm_struct.
0084  * @mm: The address space to pin.
0085  *
0086  * Make sure that the address space of the given &struct mm_struct doesn't
0087  * go away. This does not protect against parts of the address space being
0088  * modified or freed, however.
0089  *
0090  * Never use this function to pin this address space for an
0091  * unbounded/indefinite amount of time.
0092  *
0093  * Use mmput() to release the reference acquired by mmget().
0094  *
0095  * See also <Documentation/mm/active_mm.rst> for an in-depth explanation
0096  * of &mm_struct.mm_count vs &mm_struct.mm_users.
0097  */
0098 static inline void mmget(struct mm_struct *mm)
0099 {
0100     atomic_inc(&mm->mm_users);
0101 }
0102 
0103 static inline bool mmget_not_zero(struct mm_struct *mm)
0104 {
0105     return atomic_inc_not_zero(&mm->mm_users);
0106 }
0107 
0108 /* mmput gets rid of the mappings and all user-space */
0109 extern void mmput(struct mm_struct *);
0110 #ifdef CONFIG_MMU
0111 /* same as above but performs the slow path from the async context. Can
0112  * be called from the atomic context as well
0113  */
0114 void mmput_async(struct mm_struct *);
0115 #endif
0116 
0117 /* Grab a reference to a task's mm, if it is not already going away */
0118 extern struct mm_struct *get_task_mm(struct task_struct *task);
0119 /*
0120  * Grab a reference to a task's mm, if it is not already going away
0121  * and ptrace_may_access with the mode parameter passed to it
0122  * succeeds.
0123  */
0124 extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode);
0125 /* Remove the current tasks stale references to the old mm_struct on exit() */
0126 extern void exit_mm_release(struct task_struct *, struct mm_struct *);
0127 /* Remove the current tasks stale references to the old mm_struct on exec() */
0128 extern void exec_mm_release(struct task_struct *, struct mm_struct *);
0129 
0130 #ifdef CONFIG_MEMCG
0131 extern void mm_update_next_owner(struct mm_struct *mm);
0132 #else
0133 static inline void mm_update_next_owner(struct mm_struct *mm)
0134 {
0135 }
0136 #endif /* CONFIG_MEMCG */
0137 
0138 #ifdef CONFIG_MMU
0139 #ifndef arch_get_mmap_end
0140 #define arch_get_mmap_end(addr, len, flags) (TASK_SIZE)
0141 #endif
0142 
0143 #ifndef arch_get_mmap_base
0144 #define arch_get_mmap_base(addr, base) (base)
0145 #endif
0146 
0147 extern void arch_pick_mmap_layout(struct mm_struct *mm,
0148                   struct rlimit *rlim_stack);
0149 extern unsigned long
0150 arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
0151                unsigned long, unsigned long);
0152 extern unsigned long
0153 arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
0154               unsigned long len, unsigned long pgoff,
0155               unsigned long flags);
0156 
0157 unsigned long
0158 generic_get_unmapped_area(struct file *filp, unsigned long addr,
0159               unsigned long len, unsigned long pgoff,
0160               unsigned long flags);
0161 unsigned long
0162 generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
0163                   unsigned long len, unsigned long pgoff,
0164                   unsigned long flags);
0165 #else
0166 static inline void arch_pick_mmap_layout(struct mm_struct *mm,
0167                      struct rlimit *rlim_stack) {}
0168 #endif
0169 
0170 static inline bool in_vfork(struct task_struct *tsk)
0171 {
0172     bool ret;
0173 
0174     /*
0175      * need RCU to access ->real_parent if CLONE_VM was used along with
0176      * CLONE_PARENT.
0177      *
0178      * We check real_parent->mm == tsk->mm because CLONE_VFORK does not
0179      * imply CLONE_VM
0180      *
0181      * CLONE_VFORK can be used with CLONE_PARENT/CLONE_THREAD and thus
0182      * ->real_parent is not necessarily the task doing vfork(), so in
0183      * theory we can't rely on task_lock() if we want to dereference it.
0184      *
0185      * And in this case we can't trust the real_parent->mm == tsk->mm
0186      * check, it can be false negative. But we do not care, if init or
0187      * another oom-unkillable task does this it should blame itself.
0188      */
0189     rcu_read_lock();
0190     ret = tsk->vfork_done &&
0191             rcu_dereference(tsk->real_parent)->mm == tsk->mm;
0192     rcu_read_unlock();
0193 
0194     return ret;
0195 }
0196 
0197 /*
0198  * Applies per-task gfp context to the given allocation flags.
0199  * PF_MEMALLOC_NOIO implies GFP_NOIO
0200  * PF_MEMALLOC_NOFS implies GFP_NOFS
0201  * PF_MEMALLOC_PIN  implies !GFP_MOVABLE
0202  */
0203 static inline gfp_t current_gfp_context(gfp_t flags)
0204 {
0205     unsigned int pflags = READ_ONCE(current->flags);
0206 
0207     if (unlikely(pflags & (PF_MEMALLOC_NOIO | PF_MEMALLOC_NOFS | PF_MEMALLOC_PIN))) {
0208         /*
0209          * NOIO implies both NOIO and NOFS and it is a weaker context
0210          * so always make sure it makes precedence
0211          */
0212         if (pflags & PF_MEMALLOC_NOIO)
0213             flags &= ~(__GFP_IO | __GFP_FS);
0214         else if (pflags & PF_MEMALLOC_NOFS)
0215             flags &= ~__GFP_FS;
0216 
0217         if (pflags & PF_MEMALLOC_PIN)
0218             flags &= ~__GFP_MOVABLE;
0219     }
0220     return flags;
0221 }
0222 
0223 #ifdef CONFIG_LOCKDEP
0224 extern void __fs_reclaim_acquire(unsigned long ip);
0225 extern void __fs_reclaim_release(unsigned long ip);
0226 extern void fs_reclaim_acquire(gfp_t gfp_mask);
0227 extern void fs_reclaim_release(gfp_t gfp_mask);
0228 #else
0229 static inline void __fs_reclaim_acquire(unsigned long ip) { }
0230 static inline void __fs_reclaim_release(unsigned long ip) { }
0231 static inline void fs_reclaim_acquire(gfp_t gfp_mask) { }
0232 static inline void fs_reclaim_release(gfp_t gfp_mask) { }
0233 #endif
0234 
0235 /* Any memory-allocation retry loop should use
0236  * memalloc_retry_wait(), and pass the flags for the most
0237  * constrained allocation attempt that might have failed.
0238  * This provides useful documentation of where loops are,
0239  * and a central place to fine tune the waiting as the MM
0240  * implementation changes.
0241  */
0242 static inline void memalloc_retry_wait(gfp_t gfp_flags)
0243 {
0244     /* We use io_schedule_timeout because waiting for memory
0245      * typically included waiting for dirty pages to be
0246      * written out, which requires IO.
0247      */
0248     __set_current_state(TASK_UNINTERRUPTIBLE);
0249     gfp_flags = current_gfp_context(gfp_flags);
0250     if (gfpflags_allow_blocking(gfp_flags) &&
0251         !(gfp_flags & __GFP_NORETRY))
0252         /* Probably waited already, no need for much more */
0253         io_schedule_timeout(1);
0254     else
0255         /* Probably didn't wait, and has now released a lock,
0256          * so now is a good time to wait
0257          */
0258         io_schedule_timeout(HZ/50);
0259 }
0260 
0261 /**
0262  * might_alloc - Mark possible allocation sites
0263  * @gfp_mask: gfp_t flags that would be used to allocate
0264  *
0265  * Similar to might_sleep() and other annotations, this can be used in functions
0266  * that might allocate, but often don't. Compiles to nothing without
0267  * CONFIG_LOCKDEP. Includes a conditional might_sleep() if @gfp allows blocking.
0268  */
0269 static inline void might_alloc(gfp_t gfp_mask)
0270 {
0271     fs_reclaim_acquire(gfp_mask);
0272     fs_reclaim_release(gfp_mask);
0273 
0274     might_sleep_if(gfpflags_allow_blocking(gfp_mask));
0275 }
0276 
0277 /**
0278  * memalloc_noio_save - Marks implicit GFP_NOIO allocation scope.
0279  *
0280  * This functions marks the beginning of the GFP_NOIO allocation scope.
0281  * All further allocations will implicitly drop __GFP_IO flag and so
0282  * they are safe for the IO critical section from the allocation recursion
0283  * point of view. Use memalloc_noio_restore to end the scope with flags
0284  * returned by this function.
0285  *
0286  * This function is safe to be used from any context.
0287  */
0288 static inline unsigned int memalloc_noio_save(void)
0289 {
0290     unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
0291     current->flags |= PF_MEMALLOC_NOIO;
0292     return flags;
0293 }
0294 
0295 /**
0296  * memalloc_noio_restore - Ends the implicit GFP_NOIO scope.
0297  * @flags: Flags to restore.
0298  *
0299  * Ends the implicit GFP_NOIO scope started by memalloc_noio_save function.
0300  * Always make sure that the given flags is the return value from the
0301  * pairing memalloc_noio_save call.
0302  */
0303 static inline void memalloc_noio_restore(unsigned int flags)
0304 {
0305     current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
0306 }
0307 
0308 /**
0309  * memalloc_nofs_save - Marks implicit GFP_NOFS allocation scope.
0310  *
0311  * This functions marks the beginning of the GFP_NOFS allocation scope.
0312  * All further allocations will implicitly drop __GFP_FS flag and so
0313  * they are safe for the FS critical section from the allocation recursion
0314  * point of view. Use memalloc_nofs_restore to end the scope with flags
0315  * returned by this function.
0316  *
0317  * This function is safe to be used from any context.
0318  */
0319 static inline unsigned int memalloc_nofs_save(void)
0320 {
0321     unsigned int flags = current->flags & PF_MEMALLOC_NOFS;
0322     current->flags |= PF_MEMALLOC_NOFS;
0323     return flags;
0324 }
0325 
0326 /**
0327  * memalloc_nofs_restore - Ends the implicit GFP_NOFS scope.
0328  * @flags: Flags to restore.
0329  *
0330  * Ends the implicit GFP_NOFS scope started by memalloc_nofs_save function.
0331  * Always make sure that the given flags is the return value from the
0332  * pairing memalloc_nofs_save call.
0333  */
0334 static inline void memalloc_nofs_restore(unsigned int flags)
0335 {
0336     current->flags = (current->flags & ~PF_MEMALLOC_NOFS) | flags;
0337 }
0338 
0339 static inline unsigned int memalloc_noreclaim_save(void)
0340 {
0341     unsigned int flags = current->flags & PF_MEMALLOC;
0342     current->flags |= PF_MEMALLOC;
0343     return flags;
0344 }
0345 
0346 static inline void memalloc_noreclaim_restore(unsigned int flags)
0347 {
0348     current->flags = (current->flags & ~PF_MEMALLOC) | flags;
0349 }
0350 
0351 static inline unsigned int memalloc_pin_save(void)
0352 {
0353     unsigned int flags = current->flags & PF_MEMALLOC_PIN;
0354 
0355     current->flags |= PF_MEMALLOC_PIN;
0356     return flags;
0357 }
0358 
0359 static inline void memalloc_pin_restore(unsigned int flags)
0360 {
0361     current->flags = (current->flags & ~PF_MEMALLOC_PIN) | flags;
0362 }
0363 
0364 #ifdef CONFIG_MEMCG
0365 DECLARE_PER_CPU(struct mem_cgroup *, int_active_memcg);
0366 /**
0367  * set_active_memcg - Starts the remote memcg charging scope.
0368  * @memcg: memcg to charge.
0369  *
0370  * This function marks the beginning of the remote memcg charging scope. All the
0371  * __GFP_ACCOUNT allocations till the end of the scope will be charged to the
0372  * given memcg.
0373  *
0374  * NOTE: This function can nest. Users must save the return value and
0375  * reset the previous value after their own charging scope is over.
0376  */
0377 static inline struct mem_cgroup *
0378 set_active_memcg(struct mem_cgroup *memcg)
0379 {
0380     struct mem_cgroup *old;
0381 
0382     if (!in_task()) {
0383         old = this_cpu_read(int_active_memcg);
0384         this_cpu_write(int_active_memcg, memcg);
0385     } else {
0386         old = current->active_memcg;
0387         current->active_memcg = memcg;
0388     }
0389 
0390     return old;
0391 }
0392 #else
0393 static inline struct mem_cgroup *
0394 set_active_memcg(struct mem_cgroup *memcg)
0395 {
0396     return NULL;
0397 }
0398 #endif
0399 
0400 #ifdef CONFIG_MEMBARRIER
0401 enum {
0402     MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY        = (1U << 0),
0403     MEMBARRIER_STATE_PRIVATE_EXPEDITED          = (1U << 1),
0404     MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY         = (1U << 2),
0405     MEMBARRIER_STATE_GLOBAL_EXPEDITED           = (1U << 3),
0406     MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY  = (1U << 4),
0407     MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE        = (1U << 5),
0408     MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY       = (1U << 6),
0409     MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ         = (1U << 7),
0410 };
0411 
0412 enum {
0413     MEMBARRIER_FLAG_SYNC_CORE   = (1U << 0),
0414     MEMBARRIER_FLAG_RSEQ        = (1U << 1),
0415 };
0416 
0417 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
0418 #include <asm/membarrier.h>
0419 #endif
0420 
0421 static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
0422 {
0423     if (current->mm != mm)
0424         return;
0425     if (likely(!(atomic_read(&mm->membarrier_state) &
0426              MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE)))
0427         return;
0428     sync_core_before_usermode();
0429 }
0430 
0431 extern void membarrier_exec_mmap(struct mm_struct *mm);
0432 
0433 extern void membarrier_update_current_mm(struct mm_struct *next_mm);
0434 
0435 #else
0436 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
0437 static inline void membarrier_arch_switch_mm(struct mm_struct *prev,
0438                          struct mm_struct *next,
0439                          struct task_struct *tsk)
0440 {
0441 }
0442 #endif
0443 static inline void membarrier_exec_mmap(struct mm_struct *mm)
0444 {
0445 }
0446 static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
0447 {
0448 }
0449 static inline void membarrier_update_current_mm(struct mm_struct *next_mm)
0450 {
0451 }
0452 #endif
0453 
0454 #ifdef CONFIG_IOMMU_SVA
0455 static inline void mm_pasid_init(struct mm_struct *mm)
0456 {
0457     mm->pasid = INVALID_IOASID;
0458 }
0459 
0460 /* Associate a PASID with an mm_struct: */
0461 static inline void mm_pasid_set(struct mm_struct *mm, u32 pasid)
0462 {
0463     mm->pasid = pasid;
0464 }
0465 
0466 static inline void mm_pasid_drop(struct mm_struct *mm)
0467 {
0468     if (pasid_valid(mm->pasid)) {
0469         ioasid_free(mm->pasid);
0470         mm->pasid = INVALID_IOASID;
0471     }
0472 }
0473 #else
0474 static inline void mm_pasid_init(struct mm_struct *mm) {}
0475 static inline void mm_pasid_set(struct mm_struct *mm, u32 pasid) {}
0476 static inline void mm_pasid_drop(struct mm_struct *mm) {}
0477 #endif
0478 
0479 #endif /* _LINUX_SCHED_MM_H */