1
2#ifndef _LINUX_SCHED_MM_H
3#define _LINUX_SCHED_MM_H
4
5#include <linux/kernel.h>
6#include <linux/atomic.h>
7#include <linux/sched.h>
8#include <linux/mm_types.h>
9#include <linux/gfp.h>
10#include <linux/sync_core.h>
11
12
13
14
15extern struct mm_struct *mm_alloc(void);
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34static inline void mmgrab(struct mm_struct *mm)
35{
36 atomic_inc(&mm->mm_count);
37}
38
39extern void __mmdrop(struct mm_struct *mm);
40
41static inline void mmdrop(struct mm_struct *mm)
42{
43
44
45
46
47
48 if (unlikely(atomic_dec_and_test(&mm->mm_count)))
49 __mmdrop(mm);
50}
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72static inline bool mmget_still_valid(struct mm_struct *mm)
73{
74 return likely(!mm->core_state);
75}
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93static inline void mmget(struct mm_struct *mm)
94{
95 atomic_inc(&mm->mm_users);
96}
97
98static inline bool mmget_not_zero(struct mm_struct *mm)
99{
100 return atomic_inc_not_zero(&mm->mm_users);
101}
102
103
104extern void mmput(struct mm_struct *);
105#ifdef CONFIG_MMU
106
107
108
109void mmput_async(struct mm_struct *);
110#endif
111
112
113extern struct mm_struct *get_task_mm(struct task_struct *task);
114
115
116
117
118
119extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode);
120
121extern void mm_release(struct task_struct *, struct mm_struct *);
122
123#ifdef CONFIG_MEMCG
124extern void mm_update_next_owner(struct mm_struct *mm);
125#else
126static inline void mm_update_next_owner(struct mm_struct *mm)
127{
128}
129#endif
130
131#ifdef CONFIG_MMU
132extern void arch_pick_mmap_layout(struct mm_struct *mm,
133 struct rlimit *rlim_stack);
134extern unsigned long
135arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
136 unsigned long, unsigned long);
137extern unsigned long
138arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
139 unsigned long len, unsigned long pgoff,
140 unsigned long flags);
141#else
142static inline void arch_pick_mmap_layout(struct mm_struct *mm,
143 struct rlimit *rlim_stack) {}
144#endif
145
146static inline bool in_vfork(struct task_struct *tsk)
147{
148 bool ret;
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 rcu_read_lock();
166 ret = tsk->vfork_done && tsk->real_parent->mm == tsk->mm;
167 rcu_read_unlock();
168
169 return ret;
170}
171
172
173
174
175
176
177
178static inline gfp_t current_gfp_context(gfp_t flags)
179{
180 if (unlikely(current->flags &
181 (PF_MEMALLOC_NOIO | PF_MEMALLOC_NOFS | PF_MEMALLOC_NOCMA))) {
182
183
184
185
186 if (current->flags & PF_MEMALLOC_NOIO)
187 flags &= ~(__GFP_IO | __GFP_FS);
188 else if (current->flags & PF_MEMALLOC_NOFS)
189 flags &= ~__GFP_FS;
190#ifdef CONFIG_CMA
191 if (current->flags & PF_MEMALLOC_NOCMA)
192 flags &= ~__GFP_MOVABLE;
193#endif
194 }
195 return flags;
196}
197
198#ifdef CONFIG_LOCKDEP
199extern void __fs_reclaim_acquire(void);
200extern void __fs_reclaim_release(void);
201extern void fs_reclaim_acquire(gfp_t gfp_mask);
202extern void fs_reclaim_release(gfp_t gfp_mask);
203#else
204static inline void __fs_reclaim_acquire(void) { }
205static inline void __fs_reclaim_release(void) { }
206static inline void fs_reclaim_acquire(gfp_t gfp_mask) { }
207static inline void fs_reclaim_release(gfp_t gfp_mask) { }
208#endif
209
210
211
212
213
214
215
216
217
218
219
220
221static inline unsigned int memalloc_noio_save(void)
222{
223 unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
224 current->flags |= PF_MEMALLOC_NOIO;
225 return flags;
226}
227
228
229
230
231
232
233
234
235
236static inline void memalloc_noio_restore(unsigned int flags)
237{
238 current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
239}
240
241
242
243
244
245
246
247
248
249
250
251
252static inline unsigned int memalloc_nofs_save(void)
253{
254 unsigned int flags = current->flags & PF_MEMALLOC_NOFS;
255 current->flags |= PF_MEMALLOC_NOFS;
256 return flags;
257}
258
259
260
261
262
263
264
265
266
267static inline void memalloc_nofs_restore(unsigned int flags)
268{
269 current->flags = (current->flags & ~PF_MEMALLOC_NOFS) | flags;
270}
271
272static inline unsigned int memalloc_noreclaim_save(void)
273{
274 unsigned int flags = current->flags & PF_MEMALLOC;
275 current->flags |= PF_MEMALLOC;
276 return flags;
277}
278
279static inline void memalloc_noreclaim_restore(unsigned int flags)
280{
281 current->flags = (current->flags & ~PF_MEMALLOC) | flags;
282}
283
284#ifdef CONFIG_CMA
285static inline unsigned int memalloc_nocma_save(void)
286{
287 unsigned int flags = current->flags & PF_MEMALLOC_NOCMA;
288
289 current->flags |= PF_MEMALLOC_NOCMA;
290 return flags;
291}
292
293static inline void memalloc_nocma_restore(unsigned int flags)
294{
295 current->flags = (current->flags & ~PF_MEMALLOC_NOCMA) | flags;
296}
297#else
298static inline unsigned int memalloc_nocma_save(void)
299{
300 return 0;
301}
302
303static inline void memalloc_nocma_restore(unsigned int flags)
304{
305}
306#endif
307
308#ifdef CONFIG_MEMCG
309
310
311
312
313
314
315
316
317
318
319static inline void memalloc_use_memcg(struct mem_cgroup *memcg)
320{
321 WARN_ON_ONCE(current->active_memcg);
322 current->active_memcg = memcg;
323}
324
325
326
327
328
329
330
331static inline void memalloc_unuse_memcg(void)
332{
333 current->active_memcg = NULL;
334}
335#else
336static inline void memalloc_use_memcg(struct mem_cgroup *memcg)
337{
338}
339
340static inline void memalloc_unuse_memcg(void)
341{
342}
343#endif
344
345#ifdef CONFIG_MEMBARRIER
346enum {
347 MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY = (1U << 0),
348 MEMBARRIER_STATE_PRIVATE_EXPEDITED = (1U << 1),
349 MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY = (1U << 2),
350 MEMBARRIER_STATE_GLOBAL_EXPEDITED = (1U << 3),
351 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY = (1U << 4),
352 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE = (1U << 5),
353};
354
355enum {
356 MEMBARRIER_FLAG_SYNC_CORE = (1U << 0),
357};
358
359#ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
360#include <asm/membarrier.h>
361#endif
362
363static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
364{
365 if (current->mm != mm)
366 return;
367 if (likely(!(atomic_read(&mm->membarrier_state) &
368 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE)))
369 return;
370 sync_core_before_usermode();
371}
372
373extern void membarrier_exec_mmap(struct mm_struct *mm);
374
375#else
376#ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
377static inline void membarrier_arch_switch_mm(struct mm_struct *prev,
378 struct mm_struct *next,
379 struct task_struct *tsk)
380{
381}
382#endif
383static inline void membarrier_exec_mmap(struct mm_struct *mm)
384{
385}
386static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
387{
388}
389#endif
390
391#endif
392