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
68static inline void mmget(struct mm_struct *mm)
69{
70 atomic_inc(&mm->mm_users);
71}
72
73static inline bool mmget_not_zero(struct mm_struct *mm)
74{
75 return atomic_inc_not_zero(&mm->mm_users);
76}
77
78
79extern void mmput(struct mm_struct *);
80#ifdef CONFIG_MMU
81
82
83
84void mmput_async(struct mm_struct *);
85#endif
86
87
88extern struct mm_struct *get_task_mm(struct task_struct *task);
89
90
91
92
93
94extern struct mm_struct *mm_access(struct task_struct *task, unsigned int mode);
95
96extern void mm_release(struct task_struct *, struct mm_struct *);
97
98#ifdef CONFIG_MEMCG
99extern void mm_update_next_owner(struct mm_struct *mm);
100#else
101static inline void mm_update_next_owner(struct mm_struct *mm)
102{
103}
104#endif
105
106#ifdef CONFIG_MMU
107extern void arch_pick_mmap_layout(struct mm_struct *mm,
108 struct rlimit *rlim_stack);
109extern unsigned long
110arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
111 unsigned long, unsigned long);
112extern unsigned long
113arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
114 unsigned long len, unsigned long pgoff,
115 unsigned long flags);
116#else
117static inline void arch_pick_mmap_layout(struct mm_struct *mm,
118 struct rlimit *rlim_stack) {}
119#endif
120
121static inline bool in_vfork(struct task_struct *tsk)
122{
123 bool ret;
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 rcu_read_lock();
141 ret = tsk->vfork_done && tsk->real_parent->mm == tsk->mm;
142 rcu_read_unlock();
143
144 return ret;
145}
146
147
148
149
150
151
152static inline gfp_t current_gfp_context(gfp_t flags)
153{
154
155
156
157
158 if (unlikely(current->flags & PF_MEMALLOC_NOIO))
159 flags &= ~(__GFP_IO | __GFP_FS);
160 else if (unlikely(current->flags & PF_MEMALLOC_NOFS))
161 flags &= ~__GFP_FS;
162 return flags;
163}
164
165#ifdef CONFIG_LOCKDEP
166extern void __fs_reclaim_acquire(void);
167extern void __fs_reclaim_release(void);
168extern void fs_reclaim_acquire(gfp_t gfp_mask);
169extern void fs_reclaim_release(gfp_t gfp_mask);
170#else
171static inline void __fs_reclaim_acquire(void) { }
172static inline void __fs_reclaim_release(void) { }
173static inline void fs_reclaim_acquire(gfp_t gfp_mask) { }
174static inline void fs_reclaim_release(gfp_t gfp_mask) { }
175#endif
176
177
178
179
180
181
182
183
184
185
186
187
188static inline unsigned int memalloc_noio_save(void)
189{
190 unsigned int flags = current->flags & PF_MEMALLOC_NOIO;
191 current->flags |= PF_MEMALLOC_NOIO;
192 return flags;
193}
194
195
196
197
198
199
200
201
202
203static inline void memalloc_noio_restore(unsigned int flags)
204{
205 current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags;
206}
207
208
209
210
211
212
213
214
215
216
217
218
219static inline unsigned int memalloc_nofs_save(void)
220{
221 unsigned int flags = current->flags & PF_MEMALLOC_NOFS;
222 current->flags |= PF_MEMALLOC_NOFS;
223 return flags;
224}
225
226
227
228
229
230
231
232
233
234static inline void memalloc_nofs_restore(unsigned int flags)
235{
236 current->flags = (current->flags & ~PF_MEMALLOC_NOFS) | flags;
237}
238
239static inline unsigned int memalloc_noreclaim_save(void)
240{
241 unsigned int flags = current->flags & PF_MEMALLOC;
242 current->flags |= PF_MEMALLOC;
243 return flags;
244}
245
246static inline void memalloc_noreclaim_restore(unsigned int flags)
247{
248 current->flags = (current->flags & ~PF_MEMALLOC) | flags;
249}
250
251#ifdef CONFIG_MEMCG
252
253
254
255
256
257
258
259
260
261
262static inline void memalloc_use_memcg(struct mem_cgroup *memcg)
263{
264 WARN_ON_ONCE(current->active_memcg);
265 current->active_memcg = memcg;
266}
267
268
269
270
271
272
273
274static inline void memalloc_unuse_memcg(void)
275{
276 current->active_memcg = NULL;
277}
278#else
279static inline void memalloc_use_memcg(struct mem_cgroup *memcg)
280{
281}
282
283static inline void memalloc_unuse_memcg(void)
284{
285}
286#endif
287
288#ifdef CONFIG_MEMBARRIER
289enum {
290 MEMBARRIER_STATE_PRIVATE_EXPEDITED_READY = (1U << 0),
291 MEMBARRIER_STATE_PRIVATE_EXPEDITED = (1U << 1),
292 MEMBARRIER_STATE_GLOBAL_EXPEDITED_READY = (1U << 2),
293 MEMBARRIER_STATE_GLOBAL_EXPEDITED = (1U << 3),
294 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY = (1U << 4),
295 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE = (1U << 5),
296};
297
298enum {
299 MEMBARRIER_FLAG_SYNC_CORE = (1U << 0),
300};
301
302#ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
303#include <asm/membarrier.h>
304#endif
305
306static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
307{
308 if (likely(!(atomic_read(&mm->membarrier_state) &
309 MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE)))
310 return;
311 sync_core_before_usermode();
312}
313
314static inline void membarrier_execve(struct task_struct *t)
315{
316 atomic_set(&t->mm->membarrier_state, 0);
317}
318#else
319#ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
320static inline void membarrier_arch_switch_mm(struct mm_struct *prev,
321 struct mm_struct *next,
322 struct task_struct *tsk)
323{
324}
325#endif
326static inline void membarrier_execve(struct task_struct *t)
327{
328}
329static inline void membarrier_mm_sync_core_before_usermode(struct mm_struct *mm)
330{
331}
332#endif
333
334#endif
335