]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Simple NUMA memory policy for the Linux kernel. | |
3 | * | |
4 | * Copyright 2003,2004 Andi Kleen, SuSE Labs. | |
8bccd85f | 5 | * (C) Copyright 2005 Christoph Lameter, Silicon Graphics, Inc. |
1da177e4 LT |
6 | * Subject to the GNU Public License, version 2. |
7 | * | |
8 | * NUMA policy allows the user to give hints in which node(s) memory should | |
9 | * be allocated. | |
10 | * | |
11 | * Support four policies per VMA and per process: | |
12 | * | |
13 | * The VMA policy has priority over the process policy for a page fault. | |
14 | * | |
15 | * interleave Allocate memory interleaved over a set of nodes, | |
16 | * with normal fallback if it fails. | |
17 | * For VMA based allocations this interleaves based on the | |
18 | * offset into the backing object or offset into the mapping | |
19 | * for anonymous memory. For process policy an process counter | |
20 | * is used. | |
8bccd85f | 21 | * |
1da177e4 LT |
22 | * bind Only allocate memory on a specific set of nodes, |
23 | * no fallback. | |
8bccd85f CL |
24 | * FIXME: memory is allocated starting with the first node |
25 | * to the last. It would be better if bind would truly restrict | |
26 | * the allocation to memory nodes instead | |
27 | * | |
1da177e4 | 28 | * preferred Try a specific node first before normal fallback. |
00ef2d2f | 29 | * As a special case NUMA_NO_NODE here means do the allocation |
1da177e4 LT |
30 | * on the local CPU. This is normally identical to default, |
31 | * but useful to set in a VMA when you have a non default | |
32 | * process policy. | |
8bccd85f | 33 | * |
1da177e4 LT |
34 | * default Allocate on the local node first, or when on a VMA |
35 | * use the process policy. This is what Linux always did | |
36 | * in a NUMA aware kernel and still does by, ahem, default. | |
37 | * | |
38 | * The process policy is applied for most non interrupt memory allocations | |
39 | * in that process' context. Interrupts ignore the policies and always | |
40 | * try to allocate on the local CPU. The VMA policy is only applied for memory | |
41 | * allocations for a VMA in the VM. | |
42 | * | |
43 | * Currently there are a few corner cases in swapping where the policy | |
44 | * is not applied, but the majority should be handled. When process policy | |
45 | * is used it is not remembered over swap outs/swap ins. | |
46 | * | |
47 | * Only the highest zone in the zone hierarchy gets policied. Allocations | |
48 | * requesting a lower zone just use default policy. This implies that | |
49 | * on systems with highmem kernel lowmem allocation don't get policied. | |
50 | * Same with GFP_DMA allocations. | |
51 | * | |
52 | * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between | |
53 | * all users and remembered even when nobody has memory mapped. | |
54 | */ | |
55 | ||
56 | /* Notebook: | |
57 | fix mmap readahead to honour policy and enable policy for any page cache | |
58 | object | |
59 | statistics for bigpages | |
60 | global policy for page cache? currently it uses process policy. Requires | |
61 | first item above. | |
62 | handle mremap for shared memory (currently ignored for the policy) | |
63 | grows down? | |
64 | make bind policy root only? It can trigger oom much faster and the | |
65 | kernel is not always grateful with that. | |
1da177e4 LT |
66 | */ |
67 | ||
b1de0d13 MH |
68 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
69 | ||
1da177e4 LT |
70 | #include <linux/mempolicy.h> |
71 | #include <linux/mm.h> | |
72 | #include <linux/highmem.h> | |
73 | #include <linux/hugetlb.h> | |
74 | #include <linux/kernel.h> | |
75 | #include <linux/sched.h> | |
6e84f315 | 76 | #include <linux/sched/mm.h> |
6a3827d7 | 77 | #include <linux/sched/numa_balancing.h> |
f719ff9b | 78 | #include <linux/sched/task.h> |
1da177e4 LT |
79 | #include <linux/nodemask.h> |
80 | #include <linux/cpuset.h> | |
1da177e4 LT |
81 | #include <linux/slab.h> |
82 | #include <linux/string.h> | |
b95f1b31 | 83 | #include <linux/export.h> |
b488893a | 84 | #include <linux/nsproxy.h> |
1da177e4 LT |
85 | #include <linux/interrupt.h> |
86 | #include <linux/init.h> | |
87 | #include <linux/compat.h> | |
31367466 | 88 | #include <linux/ptrace.h> |
dc9aa5b9 | 89 | #include <linux/swap.h> |
1a75a6c8 CL |
90 | #include <linux/seq_file.h> |
91 | #include <linux/proc_fs.h> | |
b20a3503 | 92 | #include <linux/migrate.h> |
62b61f61 | 93 | #include <linux/ksm.h> |
95a402c3 | 94 | #include <linux/rmap.h> |
86c3a764 | 95 | #include <linux/security.h> |
dbcb0f19 | 96 | #include <linux/syscalls.h> |
095f1fc4 | 97 | #include <linux/ctype.h> |
6d9c285a | 98 | #include <linux/mm_inline.h> |
b24f53a0 | 99 | #include <linux/mmu_notifier.h> |
b1de0d13 | 100 | #include <linux/printk.h> |
c8633798 | 101 | #include <linux/swapops.h> |
dc9aa5b9 | 102 | |
1da177e4 | 103 | #include <asm/tlbflush.h> |
7c0f6ba6 | 104 | #include <linux/uaccess.h> |
1da177e4 | 105 | |
62695a84 NP |
106 | #include "internal.h" |
107 | ||
38e35860 | 108 | /* Internal flags */ |
dc9aa5b9 | 109 | #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for continuous vmas */ |
38e35860 | 110 | #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for nodemask */ |
dc9aa5b9 | 111 | |
fcc234f8 PE |
112 | static struct kmem_cache *policy_cache; |
113 | static struct kmem_cache *sn_cache; | |
1da177e4 | 114 | |
1da177e4 LT |
115 | /* Highest zone. An specific allocation for a zone below that is not |
116 | policied. */ | |
6267276f | 117 | enum zone_type policy_zone = 0; |
1da177e4 | 118 | |
bea904d5 LS |
119 | /* |
120 | * run-time system-wide default policy => local allocation | |
121 | */ | |
e754d79d | 122 | static struct mempolicy default_policy = { |
1da177e4 | 123 | .refcnt = ATOMIC_INIT(1), /* never free it */ |
bea904d5 | 124 | .mode = MPOL_PREFERRED, |
fc36b8d3 | 125 | .flags = MPOL_F_LOCAL, |
1da177e4 LT |
126 | }; |
127 | ||
5606e387 MG |
128 | static struct mempolicy preferred_node_policy[MAX_NUMNODES]; |
129 | ||
74d2c3a0 | 130 | struct mempolicy *get_task_policy(struct task_struct *p) |
5606e387 MG |
131 | { |
132 | struct mempolicy *pol = p->mempolicy; | |
f15ca78e | 133 | int node; |
5606e387 | 134 | |
f15ca78e ON |
135 | if (pol) |
136 | return pol; | |
5606e387 | 137 | |
f15ca78e ON |
138 | node = numa_node_id(); |
139 | if (node != NUMA_NO_NODE) { | |
140 | pol = &preferred_node_policy[node]; | |
141 | /* preferred_node_policy is not initialised early in boot */ | |
142 | if (pol->mode) | |
143 | return pol; | |
5606e387 MG |
144 | } |
145 | ||
f15ca78e | 146 | return &default_policy; |
5606e387 MG |
147 | } |
148 | ||
37012946 DR |
149 | static const struct mempolicy_operations { |
150 | int (*create)(struct mempolicy *pol, const nodemask_t *nodes); | |
213980c0 | 151 | void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes); |
37012946 DR |
152 | } mpol_ops[MPOL_MAX]; |
153 | ||
f5b087b5 DR |
154 | static inline int mpol_store_user_nodemask(const struct mempolicy *pol) |
155 | { | |
6d556294 | 156 | return pol->flags & MPOL_MODE_FLAGS; |
4c50bc01 DR |
157 | } |
158 | ||
159 | static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig, | |
160 | const nodemask_t *rel) | |
161 | { | |
162 | nodemask_t tmp; | |
163 | nodes_fold(tmp, *orig, nodes_weight(*rel)); | |
164 | nodes_onto(*ret, tmp, *rel); | |
f5b087b5 DR |
165 | } |
166 | ||
37012946 DR |
167 | static int mpol_new_interleave(struct mempolicy *pol, const nodemask_t *nodes) |
168 | { | |
169 | if (nodes_empty(*nodes)) | |
170 | return -EINVAL; | |
171 | pol->v.nodes = *nodes; | |
172 | return 0; | |
173 | } | |
174 | ||
175 | static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes) | |
176 | { | |
177 | if (!nodes) | |
fc36b8d3 | 178 | pol->flags |= MPOL_F_LOCAL; /* local allocation */ |
37012946 DR |
179 | else if (nodes_empty(*nodes)) |
180 | return -EINVAL; /* no allowed nodes */ | |
181 | else | |
182 | pol->v.preferred_node = first_node(*nodes); | |
183 | return 0; | |
184 | } | |
185 | ||
186 | static int mpol_new_bind(struct mempolicy *pol, const nodemask_t *nodes) | |
187 | { | |
859f7ef1 | 188 | if (nodes_empty(*nodes)) |
37012946 DR |
189 | return -EINVAL; |
190 | pol->v.nodes = *nodes; | |
191 | return 0; | |
192 | } | |
193 | ||
58568d2a MX |
194 | /* |
195 | * mpol_set_nodemask is called after mpol_new() to set up the nodemask, if | |
196 | * any, for the new policy. mpol_new() has already validated the nodes | |
197 | * parameter with respect to the policy mode and flags. But, we need to | |
198 | * handle an empty nodemask with MPOL_PREFERRED here. | |
199 | * | |
200 | * Must be called holding task's alloc_lock to protect task's mems_allowed | |
201 | * and mempolicy. May also be called holding the mmap_semaphore for write. | |
202 | */ | |
4bfc4495 KH |
203 | static int mpol_set_nodemask(struct mempolicy *pol, |
204 | const nodemask_t *nodes, struct nodemask_scratch *nsc) | |
58568d2a | 205 | { |
58568d2a MX |
206 | int ret; |
207 | ||
208 | /* if mode is MPOL_DEFAULT, pol is NULL. This is right. */ | |
209 | if (pol == NULL) | |
210 | return 0; | |
01f13bd6 | 211 | /* Check N_MEMORY */ |
4bfc4495 | 212 | nodes_and(nsc->mask1, |
01f13bd6 | 213 | cpuset_current_mems_allowed, node_states[N_MEMORY]); |
58568d2a MX |
214 | |
215 | VM_BUG_ON(!nodes); | |
216 | if (pol->mode == MPOL_PREFERRED && nodes_empty(*nodes)) | |
217 | nodes = NULL; /* explicit local allocation */ | |
218 | else { | |
219 | if (pol->flags & MPOL_F_RELATIVE_NODES) | |
859f7ef1 | 220 | mpol_relative_nodemask(&nsc->mask2, nodes, &nsc->mask1); |
58568d2a | 221 | else |
4bfc4495 KH |
222 | nodes_and(nsc->mask2, *nodes, nsc->mask1); |
223 | ||
58568d2a MX |
224 | if (mpol_store_user_nodemask(pol)) |
225 | pol->w.user_nodemask = *nodes; | |
226 | else | |
227 | pol->w.cpuset_mems_allowed = | |
228 | cpuset_current_mems_allowed; | |
229 | } | |
230 | ||
4bfc4495 KH |
231 | if (nodes) |
232 | ret = mpol_ops[pol->mode].create(pol, &nsc->mask2); | |
233 | else | |
234 | ret = mpol_ops[pol->mode].create(pol, NULL); | |
58568d2a MX |
235 | return ret; |
236 | } | |
237 | ||
238 | /* | |
239 | * This function just creates a new policy, does some check and simple | |
240 | * initialization. You must invoke mpol_set_nodemask() to set nodes. | |
241 | */ | |
028fec41 DR |
242 | static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags, |
243 | nodemask_t *nodes) | |
1da177e4 LT |
244 | { |
245 | struct mempolicy *policy; | |
246 | ||
028fec41 | 247 | pr_debug("setting mode %d flags %d nodes[0] %lx\n", |
00ef2d2f | 248 | mode, flags, nodes ? nodes_addr(*nodes)[0] : NUMA_NO_NODE); |
140d5a49 | 249 | |
3e1f0645 DR |
250 | if (mode == MPOL_DEFAULT) { |
251 | if (nodes && !nodes_empty(*nodes)) | |
37012946 | 252 | return ERR_PTR(-EINVAL); |
d3a71033 | 253 | return NULL; |
37012946 | 254 | } |
3e1f0645 DR |
255 | VM_BUG_ON(!nodes); |
256 | ||
257 | /* | |
258 | * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or | |
259 | * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation). | |
260 | * All other modes require a valid pointer to a non-empty nodemask. | |
261 | */ | |
262 | if (mode == MPOL_PREFERRED) { | |
263 | if (nodes_empty(*nodes)) { | |
264 | if (((flags & MPOL_F_STATIC_NODES) || | |
265 | (flags & MPOL_F_RELATIVE_NODES))) | |
266 | return ERR_PTR(-EINVAL); | |
3e1f0645 | 267 | } |
479e2802 | 268 | } else if (mode == MPOL_LOCAL) { |
8d303e44 PK |
269 | if (!nodes_empty(*nodes) || |
270 | (flags & MPOL_F_STATIC_NODES) || | |
271 | (flags & MPOL_F_RELATIVE_NODES)) | |
479e2802 PZ |
272 | return ERR_PTR(-EINVAL); |
273 | mode = MPOL_PREFERRED; | |
3e1f0645 DR |
274 | } else if (nodes_empty(*nodes)) |
275 | return ERR_PTR(-EINVAL); | |
1da177e4 LT |
276 | policy = kmem_cache_alloc(policy_cache, GFP_KERNEL); |
277 | if (!policy) | |
278 | return ERR_PTR(-ENOMEM); | |
279 | atomic_set(&policy->refcnt, 1); | |
45c4745a | 280 | policy->mode = mode; |
3e1f0645 | 281 | policy->flags = flags; |
37012946 | 282 | |
1da177e4 | 283 | return policy; |
37012946 DR |
284 | } |
285 | ||
52cd3b07 LS |
286 | /* Slow path of a mpol destructor. */ |
287 | void __mpol_put(struct mempolicy *p) | |
288 | { | |
289 | if (!atomic_dec_and_test(&p->refcnt)) | |
290 | return; | |
52cd3b07 LS |
291 | kmem_cache_free(policy_cache, p); |
292 | } | |
293 | ||
213980c0 | 294 | static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes) |
37012946 DR |
295 | { |
296 | } | |
297 | ||
213980c0 | 298 | static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes) |
37012946 DR |
299 | { |
300 | nodemask_t tmp; | |
301 | ||
302 | if (pol->flags & MPOL_F_STATIC_NODES) | |
303 | nodes_and(tmp, pol->w.user_nodemask, *nodes); | |
304 | else if (pol->flags & MPOL_F_RELATIVE_NODES) | |
305 | mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes); | |
306 | else { | |
213980c0 VB |
307 | nodes_remap(tmp, pol->v.nodes,pol->w.cpuset_mems_allowed, |
308 | *nodes); | |
c6c4a5fd | 309 | pol->w.cpuset_mems_allowed = *nodes; |
37012946 | 310 | } |
f5b087b5 | 311 | |
708c1bbc MX |
312 | if (nodes_empty(tmp)) |
313 | tmp = *nodes; | |
314 | ||
213980c0 | 315 | pol->v.nodes = tmp; |
37012946 DR |
316 | } |
317 | ||
318 | static void mpol_rebind_preferred(struct mempolicy *pol, | |
213980c0 | 319 | const nodemask_t *nodes) |
37012946 DR |
320 | { |
321 | nodemask_t tmp; | |
322 | ||
37012946 DR |
323 | if (pol->flags & MPOL_F_STATIC_NODES) { |
324 | int node = first_node(pol->w.user_nodemask); | |
325 | ||
fc36b8d3 | 326 | if (node_isset(node, *nodes)) { |
37012946 | 327 | pol->v.preferred_node = node; |
fc36b8d3 LS |
328 | pol->flags &= ~MPOL_F_LOCAL; |
329 | } else | |
330 | pol->flags |= MPOL_F_LOCAL; | |
37012946 DR |
331 | } else if (pol->flags & MPOL_F_RELATIVE_NODES) { |
332 | mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes); | |
333 | pol->v.preferred_node = first_node(tmp); | |
fc36b8d3 | 334 | } else if (!(pol->flags & MPOL_F_LOCAL)) { |
37012946 DR |
335 | pol->v.preferred_node = node_remap(pol->v.preferred_node, |
336 | pol->w.cpuset_mems_allowed, | |
337 | *nodes); | |
338 | pol->w.cpuset_mems_allowed = *nodes; | |
339 | } | |
1da177e4 LT |
340 | } |
341 | ||
708c1bbc MX |
342 | /* |
343 | * mpol_rebind_policy - Migrate a policy to a different set of nodes | |
344 | * | |
213980c0 VB |
345 | * Per-vma policies are protected by mmap_sem. Allocations using per-task |
346 | * policies are protected by task->mems_allowed_seq to prevent a premature | |
347 | * OOM/allocation failure due to parallel nodemask modification. | |
708c1bbc | 348 | */ |
213980c0 | 349 | static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask) |
1d0d2680 | 350 | { |
1d0d2680 DR |
351 | if (!pol) |
352 | return; | |
185072d9 | 353 | if (!mpol_store_user_nodemask(pol) && !(pol->flags & MPOL_F_LOCAL) && |
1d0d2680 DR |
354 | nodes_equal(pol->w.cpuset_mems_allowed, *newmask)) |
355 | return; | |
708c1bbc | 356 | |
213980c0 | 357 | mpol_ops[pol->mode].rebind(pol, newmask); |
1d0d2680 DR |
358 | } |
359 | ||
360 | /* | |
361 | * Wrapper for mpol_rebind_policy() that just requires task | |
362 | * pointer, and updates task mempolicy. | |
58568d2a MX |
363 | * |
364 | * Called with task's alloc_lock held. | |
1d0d2680 DR |
365 | */ |
366 | ||
213980c0 | 367 | void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new) |
1d0d2680 | 368 | { |
213980c0 | 369 | mpol_rebind_policy(tsk->mempolicy, new); |
1d0d2680 DR |
370 | } |
371 | ||
372 | /* | |
373 | * Rebind each vma in mm to new nodemask. | |
374 | * | |
375 | * Call holding a reference to mm. Takes mm->mmap_sem during call. | |
376 | */ | |
377 | ||
378 | void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new) | |
379 | { | |
380 | struct vm_area_struct *vma; | |
381 | ||
382 | down_write(&mm->mmap_sem); | |
383 | for (vma = mm->mmap; vma; vma = vma->vm_next) | |
213980c0 | 384 | mpol_rebind_policy(vma->vm_policy, new); |
1d0d2680 DR |
385 | up_write(&mm->mmap_sem); |
386 | } | |
387 | ||
37012946 DR |
388 | static const struct mempolicy_operations mpol_ops[MPOL_MAX] = { |
389 | [MPOL_DEFAULT] = { | |
390 | .rebind = mpol_rebind_default, | |
391 | }, | |
392 | [MPOL_INTERLEAVE] = { | |
393 | .create = mpol_new_interleave, | |
394 | .rebind = mpol_rebind_nodemask, | |
395 | }, | |
396 | [MPOL_PREFERRED] = { | |
397 | .create = mpol_new_preferred, | |
398 | .rebind = mpol_rebind_preferred, | |
399 | }, | |
400 | [MPOL_BIND] = { | |
401 | .create = mpol_new_bind, | |
402 | .rebind = mpol_rebind_nodemask, | |
403 | }, | |
404 | }; | |
405 | ||
8e35c297 | 406 | static int migrate_page_add(struct page *page, struct list_head *pagelist, |
fc301289 | 407 | unsigned long flags); |
1a75a6c8 | 408 | |
6f4576e3 NH |
409 | struct queue_pages { |
410 | struct list_head *pagelist; | |
411 | unsigned long flags; | |
412 | nodemask_t *nmask; | |
413 | struct vm_area_struct *prev; | |
414 | }; | |
415 | ||
88aaa2a1 NH |
416 | /* |
417 | * Check if the page's nid is in qp->nmask. | |
418 | * | |
419 | * If MPOL_MF_INVERT is set in qp->flags, check if the nid is | |
420 | * in the invert of qp->nmask. | |
421 | */ | |
422 | static inline bool queue_pages_required(struct page *page, | |
423 | struct queue_pages *qp) | |
424 | { | |
425 | int nid = page_to_nid(page); | |
426 | unsigned long flags = qp->flags; | |
427 | ||
428 | return node_isset(nid, *qp->nmask) == !(flags & MPOL_MF_INVERT); | |
429 | } | |
430 | ||
158030ee | 431 | /* |
73cae3f0 YS |
432 | * queue_pages_pmd() has four possible return values: |
433 | * 0 - pages are placed on the right node or queued successfully. | |
434 | * 1 - there is unmovable page, and MPOL_MF_MOVE* & MPOL_MF_STRICT were | |
435 | * specified. | |
436 | * 2 - THP was split. | |
437 | * -EIO - is migration entry or only MPOL_MF_STRICT was specified and an | |
438 | * existing page was already on a node that does not follow the | |
439 | * policy. | |
158030ee | 440 | */ |
c8633798 NH |
441 | static int queue_pages_pmd(pmd_t *pmd, spinlock_t *ptl, unsigned long addr, |
442 | unsigned long end, struct mm_walk *walk) | |
443 | { | |
444 | int ret = 0; | |
445 | struct page *page; | |
446 | struct queue_pages *qp = walk->private; | |
447 | unsigned long flags; | |
448 | ||
449 | if (unlikely(is_pmd_migration_entry(*pmd))) { | |
158030ee | 450 | ret = -EIO; |
c8633798 NH |
451 | goto unlock; |
452 | } | |
453 | page = pmd_page(*pmd); | |
454 | if (is_huge_zero_page(page)) { | |
455 | spin_unlock(ptl); | |
456 | __split_huge_pmd(walk->vma, pmd, addr, false, NULL); | |
73cae3f0 | 457 | ret = 2; |
c8633798 NH |
458 | goto out; |
459 | } | |
460 | if (!thp_migration_supported()) { | |
461 | get_page(page); | |
462 | spin_unlock(ptl); | |
463 | lock_page(page); | |
464 | ret = split_huge_page(page); | |
465 | unlock_page(page); | |
466 | put_page(page); | |
467 | goto out; | |
468 | } | |
73cae3f0 | 469 | if (!queue_pages_required(page, qp)) |
c8633798 | 470 | goto unlock; |
c8633798 | 471 | |
c8633798 NH |
472 | flags = qp->flags; |
473 | /* go to thp migration */ | |
158030ee | 474 | if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) { |
8e35c297 YS |
475 | if (!vma_migratable(walk->vma) || |
476 | migrate_page_add(page, qp->pagelist, flags)) { | |
73cae3f0 | 477 | ret = 1; |
158030ee YS |
478 | goto unlock; |
479 | } | |
158030ee YS |
480 | } else |
481 | ret = -EIO; | |
c8633798 NH |
482 | unlock: |
483 | spin_unlock(ptl); | |
484 | out: | |
485 | return ret; | |
486 | } | |
487 | ||
98094945 NH |
488 | /* |
489 | * Scan through pages checking if pages follow certain conditions, | |
490 | * and move them to the pagelist if they do. | |
73cae3f0 YS |
491 | * |
492 | * queue_pages_pte_range() has three possible return values: | |
493 | * 0 - pages are placed on the right node or queued successfully. | |
494 | * 1 - there is unmovable page, and MPOL_MF_MOVE* & MPOL_MF_STRICT were | |
495 | * specified. | |
496 | * -EIO - only MPOL_MF_STRICT was specified and an existing page was already | |
497 | * on a node that does not follow the policy. | |
98094945 | 498 | */ |
6f4576e3 NH |
499 | static int queue_pages_pte_range(pmd_t *pmd, unsigned long addr, |
500 | unsigned long end, struct mm_walk *walk) | |
1da177e4 | 501 | { |
6f4576e3 NH |
502 | struct vm_area_struct *vma = walk->vma; |
503 | struct page *page; | |
504 | struct queue_pages *qp = walk->private; | |
505 | unsigned long flags = qp->flags; | |
c8633798 | 506 | int ret; |
73cae3f0 | 507 | bool has_unmovable = false; |
91612e0d | 508 | pte_t *pte; |
705e87c0 | 509 | spinlock_t *ptl; |
941150a3 | 510 | |
c8633798 NH |
511 | ptl = pmd_trans_huge_lock(pmd, vma); |
512 | if (ptl) { | |
513 | ret = queue_pages_pmd(pmd, ptl, addr, end, walk); | |
73cae3f0 | 514 | if (ret != 2) |
158030ee | 515 | return ret; |
248db92d | 516 | } |
73cae3f0 | 517 | /* THP was split, fall through to pte walk */ |
91612e0d | 518 | |
337d9abf NH |
519 | if (pmd_trans_unstable(pmd)) |
520 | return 0; | |
248db92d | 521 | retry: |
6f4576e3 NH |
522 | pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); |
523 | for (; addr != end; pte++, addr += PAGE_SIZE) { | |
91612e0d | 524 | if (!pte_present(*pte)) |
1da177e4 | 525 | continue; |
6aab341e LT |
526 | page = vm_normal_page(vma, addr, *pte); |
527 | if (!page) | |
1da177e4 | 528 | continue; |
053837fc | 529 | /* |
62b61f61 HD |
530 | * vm_normal_page() filters out zero pages, but there might |
531 | * still be PageReserved pages to skip, perhaps in a VDSO. | |
053837fc | 532 | */ |
b79bc0a0 | 533 | if (PageReserved(page)) |
f4598c8b | 534 | continue; |
88aaa2a1 | 535 | if (!queue_pages_required(page, qp)) |
38e35860 | 536 | continue; |
c8633798 | 537 | if (PageTransCompound(page) && !thp_migration_supported()) { |
248db92d KS |
538 | get_page(page); |
539 | pte_unmap_unlock(pte, ptl); | |
540 | lock_page(page); | |
541 | ret = split_huge_page(page); | |
542 | unlock_page(page); | |
543 | put_page(page); | |
544 | /* Failed to split -- skip. */ | |
545 | if (ret) { | |
546 | pte = pte_offset_map_lock(walk->mm, pmd, | |
547 | addr, &ptl); | |
548 | continue; | |
549 | } | |
550 | goto retry; | |
551 | } | |
38e35860 | 552 | |
158030ee | 553 | if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) { |
73cae3f0 YS |
554 | /* MPOL_MF_STRICT must be specified if we get here */ |
555 | if (!vma_migratable(vma)) { | |
556 | has_unmovable = true; | |
158030ee | 557 | break; |
73cae3f0 | 558 | } |
8e35c297 YS |
559 | |
560 | /* | |
561 | * Do not abort immediately since there may be | |
562 | * temporary off LRU pages in the range. Still | |
563 | * need migrate other LRU pages. | |
564 | */ | |
565 | if (migrate_page_add(page, qp->pagelist, flags)) | |
566 | has_unmovable = true; | |
158030ee YS |
567 | } else |
568 | break; | |
6f4576e3 NH |
569 | } |
570 | pte_unmap_unlock(pte - 1, ptl); | |
571 | cond_resched(); | |
73cae3f0 YS |
572 | |
573 | if (has_unmovable) | |
574 | return 1; | |
575 | ||
158030ee | 576 | return addr != end ? -EIO : 0; |
91612e0d HD |
577 | } |
578 | ||
6f4576e3 NH |
579 | static int queue_pages_hugetlb(pte_t *pte, unsigned long hmask, |
580 | unsigned long addr, unsigned long end, | |
581 | struct mm_walk *walk) | |
e2d8cf40 NH |
582 | { |
583 | #ifdef CONFIG_HUGETLB_PAGE | |
6f4576e3 NH |
584 | struct queue_pages *qp = walk->private; |
585 | unsigned long flags = qp->flags; | |
e2d8cf40 | 586 | struct page *page; |
cb900f41 | 587 | spinlock_t *ptl; |
d4c54919 | 588 | pte_t entry; |
e2d8cf40 | 589 | |
6f4576e3 NH |
590 | ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte); |
591 | entry = huge_ptep_get(pte); | |
d4c54919 NH |
592 | if (!pte_present(entry)) |
593 | goto unlock; | |
594 | page = pte_page(entry); | |
88aaa2a1 | 595 | if (!queue_pages_required(page, qp)) |
e2d8cf40 NH |
596 | goto unlock; |
597 | /* With MPOL_MF_MOVE, we migrate only unshared hugepage. */ | |
598 | if (flags & (MPOL_MF_MOVE_ALL) || | |
599 | (flags & MPOL_MF_MOVE && page_mapcount(page) == 1)) | |
6f4576e3 | 600 | isolate_huge_page(page, qp->pagelist); |
e2d8cf40 | 601 | unlock: |
cb900f41 | 602 | spin_unlock(ptl); |
e2d8cf40 NH |
603 | #else |
604 | BUG(); | |
605 | #endif | |
91612e0d | 606 | return 0; |
1da177e4 LT |
607 | } |
608 | ||
5877231f | 609 | #ifdef CONFIG_NUMA_BALANCING |
b24f53a0 | 610 | /* |
4b10e7d5 MG |
611 | * This is used to mark a range of virtual addresses to be inaccessible. |
612 | * These are later cleared by a NUMA hinting fault. Depending on these | |
613 | * faults, pages may be migrated for better NUMA placement. | |
614 | * | |
615 | * This is assuming that NUMA faults are handled using PROT_NONE. If | |
616 | * an architecture makes a different choice, it will need further | |
617 | * changes to the core. | |
b24f53a0 | 618 | */ |
4b10e7d5 MG |
619 | unsigned long change_prot_numa(struct vm_area_struct *vma, |
620 | unsigned long addr, unsigned long end) | |
b24f53a0 | 621 | { |
4b10e7d5 | 622 | int nr_updated; |
b24f53a0 | 623 | |
4d942466 | 624 | nr_updated = change_protection(vma, addr, end, PAGE_NONE, 0, 1); |
03c5a6e1 MG |
625 | if (nr_updated) |
626 | count_vm_numa_events(NUMA_PTE_UPDATES, nr_updated); | |
b24f53a0 | 627 | |
4b10e7d5 | 628 | return nr_updated; |
b24f53a0 LS |
629 | } |
630 | #else | |
631 | static unsigned long change_prot_numa(struct vm_area_struct *vma, | |
632 | unsigned long addr, unsigned long end) | |
633 | { | |
634 | return 0; | |
635 | } | |
5877231f | 636 | #endif /* CONFIG_NUMA_BALANCING */ |
b24f53a0 | 637 | |
6f4576e3 NH |
638 | static int queue_pages_test_walk(unsigned long start, unsigned long end, |
639 | struct mm_walk *walk) | |
640 | { | |
641 | struct vm_area_struct *vma = walk->vma; | |
642 | struct queue_pages *qp = walk->private; | |
643 | unsigned long endvma = vma->vm_end; | |
644 | unsigned long flags = qp->flags; | |
645 | ||
158030ee YS |
646 | /* |
647 | * Need check MPOL_MF_STRICT to return -EIO if possible | |
648 | * regardless of vma_migratable | |
649 | */ | |
650 | if (!vma_migratable(vma) && | |
651 | !(flags & MPOL_MF_STRICT)) | |
48684a65 NH |
652 | return 1; |
653 | ||
6f4576e3 NH |
654 | if (endvma > end) |
655 | endvma = end; | |
656 | if (vma->vm_start > start) | |
657 | start = vma->vm_start; | |
658 | ||
659 | if (!(flags & MPOL_MF_DISCONTIG_OK)) { | |
660 | if (!vma->vm_next && vma->vm_end < end) | |
661 | return -EFAULT; | |
662 | if (qp->prev && qp->prev->vm_end < vma->vm_start) | |
663 | return -EFAULT; | |
664 | } | |
665 | ||
666 | qp->prev = vma; | |
667 | ||
6f4576e3 NH |
668 | if (flags & MPOL_MF_LAZY) { |
669 | /* Similar to task_numa_work, skip inaccessible VMAs */ | |
4355c018 LC |
670 | if (!is_vm_hugetlb_page(vma) && |
671 | (vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)) && | |
672 | !(vma->vm_flags & VM_MIXEDMAP)) | |
6f4576e3 NH |
673 | change_prot_numa(vma, start, endvma); |
674 | return 1; | |
675 | } | |
676 | ||
77bf45e7 | 677 | /* queue pages from current vma */ |
158030ee | 678 | if (flags & MPOL_MF_VALID) |
6f4576e3 NH |
679 | return 0; |
680 | return 1; | |
681 | } | |
682 | ||
dc9aa5b9 | 683 | /* |
98094945 NH |
684 | * Walk through page tables and collect pages to be migrated. |
685 | * | |
686 | * If pages found in a given range are on a set of nodes (determined by | |
687 | * @nodes and @flags,) it's isolated and queued to the pagelist which is | |
73cae3f0 YS |
688 | * passed via @private. |
689 | * | |
690 | * queue_pages_range() has three possible return values: | |
691 | * 1 - there is unmovable page, but MPOL_MF_MOVE* & MPOL_MF_STRICT were | |
692 | * specified. | |
693 | * 0 - queue pages successfully or no misplaced page. | |
694 | * -EIO - there is misplaced page and only MPOL_MF_STRICT was specified. | |
dc9aa5b9 | 695 | */ |
d05f0cdc | 696 | static int |
98094945 | 697 | queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, |
6f4576e3 NH |
698 | nodemask_t *nodes, unsigned long flags, |
699 | struct list_head *pagelist) | |
1da177e4 | 700 | { |
6f4576e3 NH |
701 | struct queue_pages qp = { |
702 | .pagelist = pagelist, | |
703 | .flags = flags, | |
704 | .nmask = nodes, | |
705 | .prev = NULL, | |
706 | }; | |
707 | struct mm_walk queue_pages_walk = { | |
708 | .hugetlb_entry = queue_pages_hugetlb, | |
709 | .pmd_entry = queue_pages_pte_range, | |
710 | .test_walk = queue_pages_test_walk, | |
711 | .mm = mm, | |
712 | .private = &qp, | |
713 | }; | |
714 | ||
715 | return walk_page_range(start, end, &queue_pages_walk); | |
1da177e4 LT |
716 | } |
717 | ||
869833f2 KM |
718 | /* |
719 | * Apply policy to a single VMA | |
720 | * This must be called with the mmap_sem held for writing. | |
721 | */ | |
722 | static int vma_replace_policy(struct vm_area_struct *vma, | |
723 | struct mempolicy *pol) | |
8d34694c | 724 | { |
869833f2 KM |
725 | int err; |
726 | struct mempolicy *old; | |
727 | struct mempolicy *new; | |
8d34694c KM |
728 | |
729 | pr_debug("vma %lx-%lx/%lx vm_ops %p vm_file %p set_policy %p\n", | |
730 | vma->vm_start, vma->vm_end, vma->vm_pgoff, | |
731 | vma->vm_ops, vma->vm_file, | |
732 | vma->vm_ops ? vma->vm_ops->set_policy : NULL); | |
733 | ||
869833f2 KM |
734 | new = mpol_dup(pol); |
735 | if (IS_ERR(new)) | |
736 | return PTR_ERR(new); | |
737 | ||
738 | if (vma->vm_ops && vma->vm_ops->set_policy) { | |
8d34694c | 739 | err = vma->vm_ops->set_policy(vma, new); |
869833f2 KM |
740 | if (err) |
741 | goto err_out; | |
8d34694c | 742 | } |
869833f2 KM |
743 | |
744 | old = vma->vm_policy; | |
745 | vma->vm_policy = new; /* protected by mmap_sem */ | |
746 | mpol_put(old); | |
747 | ||
748 | return 0; | |
749 | err_out: | |
750 | mpol_put(new); | |
8d34694c KM |
751 | return err; |
752 | } | |
753 | ||
1da177e4 | 754 | /* Step 2: apply policy to a range and do splits. */ |
9d8cebd4 KM |
755 | static int mbind_range(struct mm_struct *mm, unsigned long start, |
756 | unsigned long end, struct mempolicy *new_pol) | |
1da177e4 LT |
757 | { |
758 | struct vm_area_struct *next; | |
9d8cebd4 KM |
759 | struct vm_area_struct *prev; |
760 | struct vm_area_struct *vma; | |
761 | int err = 0; | |
e26a5114 | 762 | pgoff_t pgoff; |
9d8cebd4 KM |
763 | unsigned long vmstart; |
764 | unsigned long vmend; | |
1da177e4 | 765 | |
097d5910 | 766 | vma = find_vma(mm, start); |
9d8cebd4 KM |
767 | if (!vma || vma->vm_start > start) |
768 | return -EFAULT; | |
769 | ||
097d5910 | 770 | prev = vma->vm_prev; |
e26a5114 KM |
771 | if (start > vma->vm_start) |
772 | prev = vma; | |
773 | ||
9d8cebd4 | 774 | for (; vma && vma->vm_start < end; prev = vma, vma = next) { |
1da177e4 | 775 | next = vma->vm_next; |
9d8cebd4 KM |
776 | vmstart = max(start, vma->vm_start); |
777 | vmend = min(end, vma->vm_end); | |
778 | ||
e26a5114 KM |
779 | if (mpol_equal(vma_policy(vma), new_pol)) |
780 | continue; | |
781 | ||
782 | pgoff = vma->vm_pgoff + | |
783 | ((vmstart - vma->vm_start) >> PAGE_SHIFT); | |
9d8cebd4 | 784 | prev = vma_merge(mm, prev, vmstart, vmend, vma->vm_flags, |
19a809af AA |
785 | vma->anon_vma, vma->vm_file, pgoff, |
786 | new_pol, vma->vm_userfaultfd_ctx); | |
9d8cebd4 KM |
787 | if (prev) { |
788 | vma = prev; | |
789 | next = vma->vm_next; | |
3964acd0 ON |
790 | if (mpol_equal(vma_policy(vma), new_pol)) |
791 | continue; | |
792 | /* vma_merge() joined vma && vma->next, case 8 */ | |
793 | goto replace; | |
9d8cebd4 KM |
794 | } |
795 | if (vma->vm_start != vmstart) { | |
796 | err = split_vma(vma->vm_mm, vma, vmstart, 1); | |
797 | if (err) | |
798 | goto out; | |
799 | } | |
800 | if (vma->vm_end != vmend) { | |
801 | err = split_vma(vma->vm_mm, vma, vmend, 0); | |
802 | if (err) | |
803 | goto out; | |
804 | } | |
3964acd0 | 805 | replace: |
869833f2 | 806 | err = vma_replace_policy(vma, new_pol); |
8d34694c KM |
807 | if (err) |
808 | goto out; | |
1da177e4 | 809 | } |
9d8cebd4 KM |
810 | |
811 | out: | |
1da177e4 LT |
812 | return err; |
813 | } | |
814 | ||
1da177e4 | 815 | /* Set the process memory policy */ |
028fec41 DR |
816 | static long do_set_mempolicy(unsigned short mode, unsigned short flags, |
817 | nodemask_t *nodes) | |
1da177e4 | 818 | { |
58568d2a | 819 | struct mempolicy *new, *old; |
4bfc4495 | 820 | NODEMASK_SCRATCH(scratch); |
58568d2a | 821 | int ret; |
1da177e4 | 822 | |
4bfc4495 KH |
823 | if (!scratch) |
824 | return -ENOMEM; | |
f4e53d91 | 825 | |
4bfc4495 KH |
826 | new = mpol_new(mode, flags, nodes); |
827 | if (IS_ERR(new)) { | |
828 | ret = PTR_ERR(new); | |
829 | goto out; | |
830 | } | |
2c7c3a7d | 831 | |
58568d2a | 832 | task_lock(current); |
4bfc4495 | 833 | ret = mpol_set_nodemask(new, nodes, scratch); |
58568d2a MX |
834 | if (ret) { |
835 | task_unlock(current); | |
58568d2a | 836 | mpol_put(new); |
4bfc4495 | 837 | goto out; |
58568d2a MX |
838 | } |
839 | old = current->mempolicy; | |
1da177e4 | 840 | current->mempolicy = new; |
45816682 VB |
841 | if (new && new->mode == MPOL_INTERLEAVE) |
842 | current->il_prev = MAX_NUMNODES-1; | |
58568d2a | 843 | task_unlock(current); |
58568d2a | 844 | mpol_put(old); |
4bfc4495 KH |
845 | ret = 0; |
846 | out: | |
847 | NODEMASK_SCRATCH_FREE(scratch); | |
848 | return ret; | |
1da177e4 LT |
849 | } |
850 | ||
bea904d5 LS |
851 | /* |
852 | * Return nodemask for policy for get_mempolicy() query | |
58568d2a MX |
853 | * |
854 | * Called with task's alloc_lock held | |
bea904d5 LS |
855 | */ |
856 | static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes) | |
1da177e4 | 857 | { |
dfcd3c0d | 858 | nodes_clear(*nodes); |
bea904d5 LS |
859 | if (p == &default_policy) |
860 | return; | |
861 | ||
45c4745a | 862 | switch (p->mode) { |
19770b32 MG |
863 | case MPOL_BIND: |
864 | /* Fall through */ | |
1da177e4 | 865 | case MPOL_INTERLEAVE: |
dfcd3c0d | 866 | *nodes = p->v.nodes; |
1da177e4 LT |
867 | break; |
868 | case MPOL_PREFERRED: | |
fc36b8d3 | 869 | if (!(p->flags & MPOL_F_LOCAL)) |
dfcd3c0d | 870 | node_set(p->v.preferred_node, *nodes); |
53f2556b | 871 | /* else return empty node mask for local allocation */ |
1da177e4 LT |
872 | break; |
873 | default: | |
874 | BUG(); | |
875 | } | |
876 | } | |
877 | ||
d4edcf0d | 878 | static int lookup_node(unsigned long addr) |
1da177e4 LT |
879 | { |
880 | struct page *p; | |
881 | int err; | |
882 | ||
768ae309 | 883 | err = get_user_pages(addr & PAGE_MASK, 1, 0, &p, NULL); |
1da177e4 LT |
884 | if (err >= 0) { |
885 | err = page_to_nid(p); | |
886 | put_page(p); | |
887 | } | |
888 | return err; | |
889 | } | |
890 | ||
1da177e4 | 891 | /* Retrieve NUMA policy */ |
dbcb0f19 AB |
892 | static long do_get_mempolicy(int *policy, nodemask_t *nmask, |
893 | unsigned long addr, unsigned long flags) | |
1da177e4 | 894 | { |
8bccd85f | 895 | int err; |
1da177e4 LT |
896 | struct mm_struct *mm = current->mm; |
897 | struct vm_area_struct *vma = NULL; | |
898 | struct mempolicy *pol = current->mempolicy; | |
899 | ||
754af6f5 LS |
900 | if (flags & |
901 | ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED)) | |
1da177e4 | 902 | return -EINVAL; |
754af6f5 LS |
903 | |
904 | if (flags & MPOL_F_MEMS_ALLOWED) { | |
905 | if (flags & (MPOL_F_NODE|MPOL_F_ADDR)) | |
906 | return -EINVAL; | |
907 | *policy = 0; /* just so it's initialized */ | |
58568d2a | 908 | task_lock(current); |
754af6f5 | 909 | *nmask = cpuset_current_mems_allowed; |
58568d2a | 910 | task_unlock(current); |
754af6f5 LS |
911 | return 0; |
912 | } | |
913 | ||
1da177e4 | 914 | if (flags & MPOL_F_ADDR) { |
bea904d5 LS |
915 | /* |
916 | * Do NOT fall back to task policy if the | |
917 | * vma/shared policy at addr is NULL. We | |
918 | * want to return MPOL_DEFAULT in this case. | |
919 | */ | |
1da177e4 LT |
920 | down_read(&mm->mmap_sem); |
921 | vma = find_vma_intersection(mm, addr, addr+1); | |
922 | if (!vma) { | |
923 | up_read(&mm->mmap_sem); | |
924 | return -EFAULT; | |
925 | } | |
926 | if (vma->vm_ops && vma->vm_ops->get_policy) | |
927 | pol = vma->vm_ops->get_policy(vma, addr); | |
928 | else | |
929 | pol = vma->vm_policy; | |
930 | } else if (addr) | |
931 | return -EINVAL; | |
932 | ||
933 | if (!pol) | |
bea904d5 | 934 | pol = &default_policy; /* indicates default behavior */ |
1da177e4 LT |
935 | |
936 | if (flags & MPOL_F_NODE) { | |
937 | if (flags & MPOL_F_ADDR) { | |
d4edcf0d | 938 | err = lookup_node(addr); |
1da177e4 LT |
939 | if (err < 0) |
940 | goto out; | |
8bccd85f | 941 | *policy = err; |
1da177e4 | 942 | } else if (pol == current->mempolicy && |
45c4745a | 943 | pol->mode == MPOL_INTERLEAVE) { |
45816682 | 944 | *policy = next_node_in(current->il_prev, pol->v.nodes); |
1da177e4 LT |
945 | } else { |
946 | err = -EINVAL; | |
947 | goto out; | |
948 | } | |
bea904d5 LS |
949 | } else { |
950 | *policy = pol == &default_policy ? MPOL_DEFAULT : | |
951 | pol->mode; | |
d79df630 DR |
952 | /* |
953 | * Internal mempolicy flags must be masked off before exposing | |
954 | * the policy to userspace. | |
955 | */ | |
956 | *policy |= (pol->flags & MPOL_MODE_FLAGS); | |
bea904d5 | 957 | } |
1da177e4 | 958 | |
1da177e4 | 959 | err = 0; |
58568d2a | 960 | if (nmask) { |
c6b6ef8b LS |
961 | if (mpol_store_user_nodemask(pol)) { |
962 | *nmask = pol->w.user_nodemask; | |
963 | } else { | |
964 | task_lock(current); | |
965 | get_policy_nodemask(pol, nmask); | |
966 | task_unlock(current); | |
967 | } | |
58568d2a | 968 | } |
1da177e4 LT |
969 | |
970 | out: | |
52cd3b07 | 971 | mpol_cond_put(pol); |
1da177e4 LT |
972 | if (vma) |
973 | up_read(¤t->mm->mmap_sem); | |
974 | return err; | |
975 | } | |
976 | ||
b20a3503 | 977 | #ifdef CONFIG_MIGRATION |
6ce3c4c0 | 978 | /* |
c8633798 | 979 | * page migration, thp tail pages can be passed. |
6ce3c4c0 | 980 | */ |
8e35c297 | 981 | static int migrate_page_add(struct page *page, struct list_head *pagelist, |
fc301289 | 982 | unsigned long flags) |
6ce3c4c0 | 983 | { |
c8633798 | 984 | struct page *head = compound_head(page); |
6ce3c4c0 | 985 | /* |
fc301289 | 986 | * Avoid migrating a page that is shared with others. |
6ce3c4c0 | 987 | */ |
c8633798 NH |
988 | if ((flags & MPOL_MF_MOVE_ALL) || page_mapcount(head) == 1) { |
989 | if (!isolate_lru_page(head)) { | |
990 | list_add_tail(&head->lru, pagelist); | |
991 | mod_node_page_state(page_pgdat(head), | |
992 | NR_ISOLATED_ANON + page_is_file_cache(head), | |
993 | hpage_nr_pages(head)); | |
8e35c297 YS |
994 | } else if (flags & MPOL_MF_STRICT) { |
995 | /* | |
996 | * Non-movable page may reach here. And, there may be | |
997 | * temporary off LRU pages or non-LRU movable pages. | |
998 | * Treat them as unmovable pages since they can't be | |
999 | * isolated, so they can't be moved at the moment. It | |
1000 | * should return -EIO for this case too. | |
1001 | */ | |
1002 | return -EIO; | |
62695a84 NP |
1003 | } |
1004 | } | |
8e35c297 YS |
1005 | |
1006 | return 0; | |
7e2ab150 | 1007 | } |
6ce3c4c0 | 1008 | |
742755a1 | 1009 | static struct page *new_node_page(struct page *page, unsigned long node, int **x) |
95a402c3 | 1010 | { |
e2d8cf40 NH |
1011 | if (PageHuge(page)) |
1012 | return alloc_huge_page_node(page_hstate(compound_head(page)), | |
1013 | node); | |
c8633798 NH |
1014 | else if (thp_migration_supported() && PageTransHuge(page)) { |
1015 | struct page *thp; | |
1016 | ||
1017 | thp = alloc_pages_node(node, | |
1018 | (GFP_TRANSHUGE | __GFP_THISNODE), | |
1019 | HPAGE_PMD_ORDER); | |
1020 | if (!thp) | |
1021 | return NULL; | |
1022 | prep_transhuge_page(thp); | |
1023 | return thp; | |
1024 | } else | |
96db800f | 1025 | return __alloc_pages_node(node, GFP_HIGHUSER_MOVABLE | |
b360edb4 | 1026 | __GFP_THISNODE, 0); |
95a402c3 CL |
1027 | } |
1028 | ||
7e2ab150 CL |
1029 | /* |
1030 | * Migrate pages from one node to a target node. | |
1031 | * Returns error or the number of pages not migrated. | |
1032 | */ | |
dbcb0f19 AB |
1033 | static int migrate_to_node(struct mm_struct *mm, int source, int dest, |
1034 | int flags) | |
7e2ab150 CL |
1035 | { |
1036 | nodemask_t nmask; | |
1037 | LIST_HEAD(pagelist); | |
1038 | int err = 0; | |
1039 | ||
1040 | nodes_clear(nmask); | |
1041 | node_set(source, nmask); | |
6ce3c4c0 | 1042 | |
08270807 MK |
1043 | /* |
1044 | * This does not "check" the range but isolates all pages that | |
1045 | * need migration. Between passing in the full user address | |
1046 | * space range and MPOL_MF_DISCONTIG_OK, this call can not fail. | |
1047 | */ | |
1048 | VM_BUG_ON(!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))); | |
98094945 | 1049 | queue_pages_range(mm, mm->mmap->vm_start, mm->task_size, &nmask, |
7e2ab150 CL |
1050 | flags | MPOL_MF_DISCONTIG_OK, &pagelist); |
1051 | ||
cf608ac1 | 1052 | if (!list_empty(&pagelist)) { |
68711a74 | 1053 | err = migrate_pages(&pagelist, new_node_page, NULL, dest, |
9c620e2b | 1054 | MIGRATE_SYNC, MR_SYSCALL); |
cf608ac1 | 1055 | if (err) |
e2d8cf40 | 1056 | putback_movable_pages(&pagelist); |
cf608ac1 | 1057 | } |
95a402c3 | 1058 | |
7e2ab150 | 1059 | return err; |
6ce3c4c0 CL |
1060 | } |
1061 | ||
39743889 | 1062 | /* |
7e2ab150 CL |
1063 | * Move pages between the two nodesets so as to preserve the physical |
1064 | * layout as much as possible. | |
39743889 CL |
1065 | * |
1066 | * Returns the number of page that could not be moved. | |
1067 | */ | |
0ce72d4f AM |
1068 | int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from, |
1069 | const nodemask_t *to, int flags) | |
39743889 | 1070 | { |
7e2ab150 | 1071 | int busy = 0; |
0aedadf9 | 1072 | int err; |
7e2ab150 | 1073 | nodemask_t tmp; |
39743889 | 1074 | |
0aedadf9 CL |
1075 | err = migrate_prep(); |
1076 | if (err) | |
1077 | return err; | |
1078 | ||
53f2556b | 1079 | down_read(&mm->mmap_sem); |
39743889 | 1080 | |
da0aa138 KM |
1081 | /* |
1082 | * Find a 'source' bit set in 'tmp' whose corresponding 'dest' | |
1083 | * bit in 'to' is not also set in 'tmp'. Clear the found 'source' | |
1084 | * bit in 'tmp', and return that <source, dest> pair for migration. | |
1085 | * The pair of nodemasks 'to' and 'from' define the map. | |
1086 | * | |
1087 | * If no pair of bits is found that way, fallback to picking some | |
1088 | * pair of 'source' and 'dest' bits that are not the same. If the | |
1089 | * 'source' and 'dest' bits are the same, this represents a node | |
1090 | * that will be migrating to itself, so no pages need move. | |
1091 | * | |
1092 | * If no bits are left in 'tmp', or if all remaining bits left | |
1093 | * in 'tmp' correspond to the same bit in 'to', return false | |
1094 | * (nothing left to migrate). | |
1095 | * | |
1096 | * This lets us pick a pair of nodes to migrate between, such that | |
1097 | * if possible the dest node is not already occupied by some other | |
1098 | * source node, minimizing the risk of overloading the memory on a | |
1099 | * node that would happen if we migrated incoming memory to a node | |
1100 | * before migrating outgoing memory source that same node. | |
1101 | * | |
1102 | * A single scan of tmp is sufficient. As we go, we remember the | |
1103 | * most recent <s, d> pair that moved (s != d). If we find a pair | |
1104 | * that not only moved, but what's better, moved to an empty slot | |
1105 | * (d is not set in tmp), then we break out then, with that pair. | |
ae0e47f0 | 1106 | * Otherwise when we finish scanning from_tmp, we at least have the |
da0aa138 KM |
1107 | * most recent <s, d> pair that moved. If we get all the way through |
1108 | * the scan of tmp without finding any node that moved, much less | |
1109 | * moved to an empty node, then there is nothing left worth migrating. | |
1110 | */ | |
d4984711 | 1111 | |
0ce72d4f | 1112 | tmp = *from; |
7e2ab150 CL |
1113 | while (!nodes_empty(tmp)) { |
1114 | int s,d; | |
b76ac7e7 | 1115 | int source = NUMA_NO_NODE; |
7e2ab150 CL |
1116 | int dest = 0; |
1117 | ||
1118 | for_each_node_mask(s, tmp) { | |
4a5b18cc LW |
1119 | |
1120 | /* | |
1121 | * do_migrate_pages() tries to maintain the relative | |
1122 | * node relationship of the pages established between | |
1123 | * threads and memory areas. | |
1124 | * | |
1125 | * However if the number of source nodes is not equal to | |
1126 | * the number of destination nodes we can not preserve | |
1127 | * this node relative relationship. In that case, skip | |
1128 | * copying memory from a node that is in the destination | |
1129 | * mask. | |
1130 | * | |
1131 | * Example: [2,3,4] -> [3,4,5] moves everything. | |
1132 | * [0-7] - > [3,4,5] moves only 0,1,2,6,7. | |
1133 | */ | |
1134 | ||
0ce72d4f AM |
1135 | if ((nodes_weight(*from) != nodes_weight(*to)) && |
1136 | (node_isset(s, *to))) | |
4a5b18cc LW |
1137 | continue; |
1138 | ||
0ce72d4f | 1139 | d = node_remap(s, *from, *to); |
7e2ab150 CL |
1140 | if (s == d) |
1141 | continue; | |
1142 | ||
1143 | source = s; /* Node moved. Memorize */ | |
1144 | dest = d; | |
1145 | ||
1146 | /* dest not in remaining from nodes? */ | |
1147 | if (!node_isset(dest, tmp)) | |
1148 | break; | |
1149 | } | |
b76ac7e7 | 1150 | if (source == NUMA_NO_NODE) |
7e2ab150 CL |
1151 | break; |
1152 | ||
1153 | node_clear(source, tmp); | |
1154 | err = migrate_to_node(mm, source, dest, flags); | |
1155 | if (err > 0) | |
1156 | busy += err; | |
1157 | if (err < 0) | |
1158 | break; | |
39743889 CL |
1159 | } |
1160 | up_read(&mm->mmap_sem); | |
7e2ab150 CL |
1161 | if (err < 0) |
1162 | return err; | |
1163 | return busy; | |
b20a3503 CL |
1164 | |
1165 | } | |
1166 | ||
3ad33b24 LS |
1167 | /* |
1168 | * Allocate a new page for page migration based on vma policy. | |
d05f0cdc | 1169 | * Start by assuming the page is mapped by the same vma as contains @start. |
3ad33b24 LS |
1170 | * Search forward from there, if not. N.B., this assumes that the |
1171 | * list of pages handed to migrate_pages()--which is how we get here-- | |
1172 | * is in virtual address order. | |
1173 | */ | |
d05f0cdc | 1174 | static struct page *new_page(struct page *page, unsigned long start, int **x) |
95a402c3 | 1175 | { |
d05f0cdc | 1176 | struct vm_area_struct *vma; |
3ad33b24 | 1177 | unsigned long uninitialized_var(address); |
95a402c3 | 1178 | |
d05f0cdc | 1179 | vma = find_vma(current->mm, start); |
3ad33b24 LS |
1180 | while (vma) { |
1181 | address = page_address_in_vma(page, vma); | |
1182 | if (address != -EFAULT) | |
1183 | break; | |
1184 | vma = vma->vm_next; | |
1185 | } | |
11c731e8 WL |
1186 | |
1187 | if (PageHuge(page)) { | |
cc81717e MH |
1188 | BUG_ON(!vma); |
1189 | return alloc_huge_page_noerr(vma, address, 1); | |
c8633798 NH |
1190 | } else if (thp_migration_supported() && PageTransHuge(page)) { |
1191 | struct page *thp; | |
1192 | ||
1193 | thp = alloc_hugepage_vma(GFP_TRANSHUGE, vma, address, | |
1194 | HPAGE_PMD_ORDER); | |
1195 | if (!thp) | |
1196 | return NULL; | |
1197 | prep_transhuge_page(thp); | |
1198 | return thp; | |
11c731e8 | 1199 | } |
0bf598d8 | 1200 | /* |
11c731e8 | 1201 | * if !vma, alloc_page_vma() will use task or system default policy |
0bf598d8 | 1202 | */ |
0f556856 MH |
1203 | return alloc_page_vma(GFP_HIGHUSER_MOVABLE | __GFP_RETRY_MAYFAIL, |
1204 | vma, address); | |
95a402c3 | 1205 | } |
b20a3503 CL |
1206 | #else |
1207 | ||
8e35c297 | 1208 | static int migrate_page_add(struct page *page, struct list_head *pagelist, |
b20a3503 CL |
1209 | unsigned long flags) |
1210 | { | |
8e35c297 | 1211 | return -EIO; |
39743889 CL |
1212 | } |
1213 | ||
0ce72d4f AM |
1214 | int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from, |
1215 | const nodemask_t *to, int flags) | |
b20a3503 CL |
1216 | { |
1217 | return -ENOSYS; | |
1218 | } | |
95a402c3 | 1219 | |
d05f0cdc | 1220 | static struct page *new_page(struct page *page, unsigned long start, int **x) |
95a402c3 CL |
1221 | { |
1222 | return NULL; | |
1223 | } | |
b20a3503 CL |
1224 | #endif |
1225 | ||
dbcb0f19 | 1226 | static long do_mbind(unsigned long start, unsigned long len, |
028fec41 DR |
1227 | unsigned short mode, unsigned short mode_flags, |
1228 | nodemask_t *nmask, unsigned long flags) | |
6ce3c4c0 | 1229 | { |
6ce3c4c0 CL |
1230 | struct mm_struct *mm = current->mm; |
1231 | struct mempolicy *new; | |
1232 | unsigned long end; | |
1233 | int err; | |
73cae3f0 | 1234 | int ret; |
6ce3c4c0 CL |
1235 | LIST_HEAD(pagelist); |
1236 | ||
b24f53a0 | 1237 | if (flags & ~(unsigned long)MPOL_MF_VALID) |
6ce3c4c0 | 1238 | return -EINVAL; |
74c00241 | 1239 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) |
6ce3c4c0 CL |
1240 | return -EPERM; |
1241 | ||
1242 | if (start & ~PAGE_MASK) | |
1243 | return -EINVAL; | |
1244 | ||
1245 | if (mode == MPOL_DEFAULT) | |
1246 | flags &= ~MPOL_MF_STRICT; | |
1247 | ||
1248 | len = (len + PAGE_SIZE - 1) & PAGE_MASK; | |
1249 | end = start + len; | |
1250 | ||
1251 | if (end < start) | |
1252 | return -EINVAL; | |
1253 | if (end == start) | |
1254 | return 0; | |
1255 | ||
028fec41 | 1256 | new = mpol_new(mode, mode_flags, nmask); |
6ce3c4c0 CL |
1257 | if (IS_ERR(new)) |
1258 | return PTR_ERR(new); | |
1259 | ||
b24f53a0 LS |
1260 | if (flags & MPOL_MF_LAZY) |
1261 | new->flags |= MPOL_F_MOF; | |
1262 | ||
6ce3c4c0 CL |
1263 | /* |
1264 | * If we are using the default policy then operation | |
1265 | * on discontinuous address spaces is okay after all | |
1266 | */ | |
1267 | if (!new) | |
1268 | flags |= MPOL_MF_DISCONTIG_OK; | |
1269 | ||
028fec41 DR |
1270 | pr_debug("mbind %lx-%lx mode:%d flags:%d nodes:%lx\n", |
1271 | start, start + len, mode, mode_flags, | |
00ef2d2f | 1272 | nmask ? nodes_addr(*nmask)[0] : NUMA_NO_NODE); |
6ce3c4c0 | 1273 | |
0aedadf9 CL |
1274 | if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) { |
1275 | ||
1276 | err = migrate_prep(); | |
1277 | if (err) | |
b05ca738 | 1278 | goto mpol_out; |
0aedadf9 | 1279 | } |
4bfc4495 KH |
1280 | { |
1281 | NODEMASK_SCRATCH(scratch); | |
1282 | if (scratch) { | |
1283 | down_write(&mm->mmap_sem); | |
1284 | task_lock(current); | |
1285 | err = mpol_set_nodemask(new, nmask, scratch); | |
1286 | task_unlock(current); | |
1287 | if (err) | |
1288 | up_write(&mm->mmap_sem); | |
1289 | } else | |
1290 | err = -ENOMEM; | |
1291 | NODEMASK_SCRATCH_FREE(scratch); | |
1292 | } | |
b05ca738 KM |
1293 | if (err) |
1294 | goto mpol_out; | |
1295 | ||
73cae3f0 | 1296 | ret = queue_pages_range(mm, start, end, nmask, |
6ce3c4c0 | 1297 | flags | MPOL_MF_INVERT, &pagelist); |
73cae3f0 YS |
1298 | |
1299 | if (ret < 0) { | |
1300 | err = -EIO; | |
1301 | goto up_out; | |
1302 | } | |
1303 | ||
1304 | err = mbind_range(mm, start, end, new); | |
7e2ab150 | 1305 | |
b24f53a0 LS |
1306 | if (!err) { |
1307 | int nr_failed = 0; | |
1308 | ||
cf608ac1 | 1309 | if (!list_empty(&pagelist)) { |
b24f53a0 | 1310 | WARN_ON_ONCE(flags & MPOL_MF_LAZY); |
d05f0cdc HD |
1311 | nr_failed = migrate_pages(&pagelist, new_page, NULL, |
1312 | start, MIGRATE_SYNC, MR_MEMPOLICY_MBIND); | |
cf608ac1 | 1313 | if (nr_failed) |
74060e4d | 1314 | putback_movable_pages(&pagelist); |
cf608ac1 | 1315 | } |
6ce3c4c0 | 1316 | |
73cae3f0 | 1317 | if ((ret > 0) || (nr_failed && (flags & MPOL_MF_STRICT))) |
6ce3c4c0 | 1318 | err = -EIO; |
ab8a3e14 | 1319 | } else |
b0e5fd73 | 1320 | putback_movable_pages(&pagelist); |
b20a3503 | 1321 | |
73cae3f0 | 1322 | up_out: |
6ce3c4c0 | 1323 | up_write(&mm->mmap_sem); |
73cae3f0 | 1324 | mpol_out: |
f0be3d32 | 1325 | mpol_put(new); |
6ce3c4c0 CL |
1326 | return err; |
1327 | } | |
1328 | ||
8bccd85f CL |
1329 | /* |
1330 | * User space interface with variable sized bitmaps for nodelists. | |
1331 | */ | |
1332 | ||
1333 | /* Copy a node mask from user space. */ | |
39743889 | 1334 | static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, |
8bccd85f CL |
1335 | unsigned long maxnode) |
1336 | { | |
1337 | unsigned long k; | |
5f39f8bf | 1338 | unsigned long t; |
8bccd85f CL |
1339 | unsigned long nlongs; |
1340 | unsigned long endmask; | |
1341 | ||
1342 | --maxnode; | |
1343 | nodes_clear(*nodes); | |
1344 | if (maxnode == 0 || !nmask) | |
1345 | return 0; | |
a9c930ba | 1346 | if (maxnode > PAGE_SIZE*BITS_PER_BYTE) |
636f13c1 | 1347 | return -EINVAL; |
8bccd85f CL |
1348 | |
1349 | nlongs = BITS_TO_LONGS(maxnode); | |
1350 | if ((maxnode % BITS_PER_LONG) == 0) | |
1351 | endmask = ~0UL; | |
1352 | else | |
1353 | endmask = (1UL << (maxnode % BITS_PER_LONG)) - 1; | |
1354 | ||
5f39f8bf YX |
1355 | /* |
1356 | * When the user specified more nodes than supported just check | |
1357 | * if the non supported part is all zero. | |
1358 | * | |
1359 | * If maxnode have more longs than MAX_NUMNODES, check | |
1360 | * the bits in that area first. And then go through to | |
1361 | * check the rest bits which equal or bigger than MAX_NUMNODES. | |
1362 | * Otherwise, just check bits [MAX_NUMNODES, maxnode). | |
1363 | */ | |
8bccd85f CL |
1364 | if (nlongs > BITS_TO_LONGS(MAX_NUMNODES)) { |
1365 | if (nlongs > PAGE_SIZE/sizeof(long)) | |
1366 | return -EINVAL; | |
1367 | for (k = BITS_TO_LONGS(MAX_NUMNODES); k < nlongs; k++) { | |
8bccd85f CL |
1368 | if (get_user(t, nmask + k)) |
1369 | return -EFAULT; | |
1370 | if (k == nlongs - 1) { | |
1371 | if (t & endmask) | |
1372 | return -EINVAL; | |
1373 | } else if (t) | |
1374 | return -EINVAL; | |
1375 | } | |
1376 | nlongs = BITS_TO_LONGS(MAX_NUMNODES); | |
1377 | endmask = ~0UL; | |
1378 | } | |
1379 | ||
5f39f8bf YX |
1380 | if (maxnode > MAX_NUMNODES && MAX_NUMNODES % BITS_PER_LONG != 0) { |
1381 | unsigned long valid_mask = endmask; | |
1382 | ||
1383 | valid_mask &= ~((1UL << (MAX_NUMNODES % BITS_PER_LONG)) - 1); | |
1384 | if (get_user(t, nmask + nlongs - 1)) | |
1385 | return -EFAULT; | |
1386 | if (t & valid_mask) | |
1387 | return -EINVAL; | |
1388 | } | |
1389 | ||
8bccd85f CL |
1390 | if (copy_from_user(nodes_addr(*nodes), nmask, nlongs*sizeof(unsigned long))) |
1391 | return -EFAULT; | |
1392 | nodes_addr(*nodes)[nlongs-1] &= endmask; | |
1393 | return 0; | |
1394 | } | |
1395 | ||
1396 | /* Copy a kernel node mask to user space */ | |
1397 | static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode, | |
1398 | nodemask_t *nodes) | |
1399 | { | |
1400 | unsigned long copy = ALIGN(maxnode-1, 64) / 8; | |
d067717d | 1401 | unsigned int nbytes = BITS_TO_LONGS(nr_node_ids) * sizeof(long); |
8bccd85f CL |
1402 | |
1403 | if (copy > nbytes) { | |
1404 | if (copy > PAGE_SIZE) | |
1405 | return -EINVAL; | |
1406 | if (clear_user((char __user *)mask + nbytes, copy - nbytes)) | |
1407 | return -EFAULT; | |
1408 | copy = nbytes; | |
1409 | } | |
1410 | return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0; | |
1411 | } | |
1412 | ||
938bb9f5 | 1413 | SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len, |
f7f28ca9 | 1414 | unsigned long, mode, const unsigned long __user *, nmask, |
938bb9f5 | 1415 | unsigned long, maxnode, unsigned, flags) |
8bccd85f CL |
1416 | { |
1417 | nodemask_t nodes; | |
1418 | int err; | |
028fec41 | 1419 | unsigned short mode_flags; |
8bccd85f | 1420 | |
028fec41 DR |
1421 | mode_flags = mode & MPOL_MODE_FLAGS; |
1422 | mode &= ~MPOL_MODE_FLAGS; | |
a3b51e01 DR |
1423 | if (mode >= MPOL_MAX) |
1424 | return -EINVAL; | |
4c50bc01 DR |
1425 | if ((mode_flags & MPOL_F_STATIC_NODES) && |
1426 | (mode_flags & MPOL_F_RELATIVE_NODES)) | |
1427 | return -EINVAL; | |
8bccd85f CL |
1428 | err = get_nodes(&nodes, nmask, maxnode); |
1429 | if (err) | |
1430 | return err; | |
028fec41 | 1431 | return do_mbind(start, len, mode, mode_flags, &nodes, flags); |
8bccd85f CL |
1432 | } |
1433 | ||
1434 | /* Set the process memory policy */ | |
23c8902d | 1435 | SYSCALL_DEFINE3(set_mempolicy, int, mode, const unsigned long __user *, nmask, |
938bb9f5 | 1436 | unsigned long, maxnode) |
8bccd85f CL |
1437 | { |
1438 | int err; | |
1439 | nodemask_t nodes; | |
028fec41 | 1440 | unsigned short flags; |
8bccd85f | 1441 | |
028fec41 DR |
1442 | flags = mode & MPOL_MODE_FLAGS; |
1443 | mode &= ~MPOL_MODE_FLAGS; | |
1444 | if ((unsigned int)mode >= MPOL_MAX) | |
8bccd85f | 1445 | return -EINVAL; |
4c50bc01 DR |
1446 | if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES)) |
1447 | return -EINVAL; | |
8bccd85f CL |
1448 | err = get_nodes(&nodes, nmask, maxnode); |
1449 | if (err) | |
1450 | return err; | |
028fec41 | 1451 | return do_set_mempolicy(mode, flags, &nodes); |
8bccd85f CL |
1452 | } |
1453 | ||
938bb9f5 HC |
1454 | SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode, |
1455 | const unsigned long __user *, old_nodes, | |
1456 | const unsigned long __user *, new_nodes) | |
39743889 | 1457 | { |
596d7cfa | 1458 | struct mm_struct *mm = NULL; |
39743889 | 1459 | struct task_struct *task; |
39743889 CL |
1460 | nodemask_t task_nodes; |
1461 | int err; | |
596d7cfa KM |
1462 | nodemask_t *old; |
1463 | nodemask_t *new; | |
1464 | NODEMASK_SCRATCH(scratch); | |
1465 | ||
1466 | if (!scratch) | |
1467 | return -ENOMEM; | |
39743889 | 1468 | |
596d7cfa KM |
1469 | old = &scratch->mask1; |
1470 | new = &scratch->mask2; | |
1471 | ||
1472 | err = get_nodes(old, old_nodes, maxnode); | |
39743889 | 1473 | if (err) |
596d7cfa | 1474 | goto out; |
39743889 | 1475 | |
596d7cfa | 1476 | err = get_nodes(new, new_nodes, maxnode); |
39743889 | 1477 | if (err) |
596d7cfa | 1478 | goto out; |
39743889 CL |
1479 | |
1480 | /* Find the mm_struct */ | |
55cfaa3c | 1481 | rcu_read_lock(); |
228ebcbe | 1482 | task = pid ? find_task_by_vpid(pid) : current; |
39743889 | 1483 | if (!task) { |
55cfaa3c | 1484 | rcu_read_unlock(); |
596d7cfa KM |
1485 | err = -ESRCH; |
1486 | goto out; | |
39743889 | 1487 | } |
3268c63e | 1488 | get_task_struct(task); |
39743889 | 1489 | |
596d7cfa | 1490 | err = -EINVAL; |
39743889 CL |
1491 | |
1492 | /* | |
31367466 OE |
1493 | * Check if this process has the right to modify the specified process. |
1494 | * Use the regular "ptrace_may_access()" checks. | |
39743889 | 1495 | */ |
31367466 | 1496 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { |
c69e8d9c | 1497 | rcu_read_unlock(); |
39743889 | 1498 | err = -EPERM; |
3268c63e | 1499 | goto out_put; |
39743889 | 1500 | } |
c69e8d9c | 1501 | rcu_read_unlock(); |
39743889 CL |
1502 | |
1503 | task_nodes = cpuset_mems_allowed(task); | |
1504 | /* Is the user allowed to access the target nodes? */ | |
596d7cfa | 1505 | if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) { |
39743889 | 1506 | err = -EPERM; |
3268c63e | 1507 | goto out_put; |
39743889 CL |
1508 | } |
1509 | ||
bfe7a667 YX |
1510 | task_nodes = cpuset_mems_allowed(current); |
1511 | nodes_and(*new, *new, task_nodes); | |
1512 | if (nodes_empty(*new)) | |
1513 | goto out_put; | |
1514 | ||
1515 | nodes_and(*new, *new, node_states[N_MEMORY]); | |
1516 | if (nodes_empty(*new)) | |
3268c63e | 1517 | goto out_put; |
3b42d28b | 1518 | |
86c3a764 DQ |
1519 | err = security_task_movememory(task); |
1520 | if (err) | |
3268c63e | 1521 | goto out_put; |
86c3a764 | 1522 | |
3268c63e CL |
1523 | mm = get_task_mm(task); |
1524 | put_task_struct(task); | |
f2a9ef88 SL |
1525 | |
1526 | if (!mm) { | |
3268c63e | 1527 | err = -EINVAL; |
f2a9ef88 SL |
1528 | goto out; |
1529 | } | |
1530 | ||
1531 | err = do_migrate_pages(mm, old, new, | |
1532 | capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE); | |
3268c63e CL |
1533 | |
1534 | mmput(mm); | |
1535 | out: | |
596d7cfa KM |
1536 | NODEMASK_SCRATCH_FREE(scratch); |
1537 | ||
39743889 | 1538 | return err; |
3268c63e CL |
1539 | |
1540 | out_put: | |
1541 | put_task_struct(task); | |
1542 | goto out; | |
1543 | ||
39743889 CL |
1544 | } |
1545 | ||
1546 | ||
8bccd85f | 1547 | /* Retrieve NUMA policy */ |
938bb9f5 HC |
1548 | SYSCALL_DEFINE5(get_mempolicy, int __user *, policy, |
1549 | unsigned long __user *, nmask, unsigned long, maxnode, | |
1550 | unsigned long, addr, unsigned long, flags) | |
8bccd85f | 1551 | { |
dbcb0f19 AB |
1552 | int err; |
1553 | int uninitialized_var(pval); | |
8bccd85f CL |
1554 | nodemask_t nodes; |
1555 | ||
d067717d | 1556 | if (nmask != NULL && maxnode < nr_node_ids) |
8bccd85f CL |
1557 | return -EINVAL; |
1558 | ||
1559 | err = do_get_mempolicy(&pval, &nodes, addr, flags); | |
1560 | ||
1561 | if (err) | |
1562 | return err; | |
1563 | ||
1564 | if (policy && put_user(pval, policy)) | |
1565 | return -EFAULT; | |
1566 | ||
1567 | if (nmask) | |
1568 | err = copy_nodes_to_user(nmask, maxnode, &nodes); | |
1569 | ||
1570 | return err; | |
1571 | } | |
1572 | ||
1da177e4 LT |
1573 | #ifdef CONFIG_COMPAT |
1574 | ||
c93e0f6c HC |
1575 | COMPAT_SYSCALL_DEFINE5(get_mempolicy, int __user *, policy, |
1576 | compat_ulong_t __user *, nmask, | |
1577 | compat_ulong_t, maxnode, | |
1578 | compat_ulong_t, addr, compat_ulong_t, flags) | |
1da177e4 LT |
1579 | { |
1580 | long err; | |
1581 | unsigned long __user *nm = NULL; | |
1582 | unsigned long nr_bits, alloc_size; | |
1583 | DECLARE_BITMAP(bm, MAX_NUMNODES); | |
1584 | ||
d067717d | 1585 | nr_bits = min_t(unsigned long, maxnode-1, nr_node_ids); |
1da177e4 LT |
1586 | alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; |
1587 | ||
1588 | if (nmask) | |
1589 | nm = compat_alloc_user_space(alloc_size); | |
1590 | ||
1591 | err = sys_get_mempolicy(policy, nm, nr_bits+1, addr, flags); | |
1592 | ||
1593 | if (!err && nmask) { | |
2bbff6c7 KH |
1594 | unsigned long copy_size; |
1595 | copy_size = min_t(unsigned long, sizeof(bm), alloc_size); | |
1596 | err = copy_from_user(bm, nm, copy_size); | |
1da177e4 LT |
1597 | /* ensure entire bitmap is zeroed */ |
1598 | err |= clear_user(nmask, ALIGN(maxnode-1, 8) / 8); | |
1599 | err |= compat_put_bitmap(nmask, bm, nr_bits); | |
1600 | } | |
1601 | ||
1602 | return err; | |
1603 | } | |
1604 | ||
c93e0f6c HC |
1605 | COMPAT_SYSCALL_DEFINE3(set_mempolicy, int, mode, compat_ulong_t __user *, nmask, |
1606 | compat_ulong_t, maxnode) | |
1da177e4 | 1607 | { |
1da177e4 LT |
1608 | unsigned long __user *nm = NULL; |
1609 | unsigned long nr_bits, alloc_size; | |
1610 | DECLARE_BITMAP(bm, MAX_NUMNODES); | |
1611 | ||
1612 | nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); | |
1613 | alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; | |
1614 | ||
1615 | if (nmask) { | |
cf01fb99 CS |
1616 | if (compat_get_bitmap(bm, nmask, nr_bits)) |
1617 | return -EFAULT; | |
1da177e4 | 1618 | nm = compat_alloc_user_space(alloc_size); |
cf01fb99 CS |
1619 | if (copy_to_user(nm, bm, alloc_size)) |
1620 | return -EFAULT; | |
1da177e4 LT |
1621 | } |
1622 | ||
1da177e4 LT |
1623 | return sys_set_mempolicy(mode, nm, nr_bits+1); |
1624 | } | |
1625 | ||
c93e0f6c HC |
1626 | COMPAT_SYSCALL_DEFINE6(mbind, compat_ulong_t, start, compat_ulong_t, len, |
1627 | compat_ulong_t, mode, compat_ulong_t __user *, nmask, | |
1628 | compat_ulong_t, maxnode, compat_ulong_t, flags) | |
1da177e4 | 1629 | { |
1da177e4 LT |
1630 | unsigned long __user *nm = NULL; |
1631 | unsigned long nr_bits, alloc_size; | |
dfcd3c0d | 1632 | nodemask_t bm; |
1da177e4 LT |
1633 | |
1634 | nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES); | |
1635 | alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8; | |
1636 | ||
1637 | if (nmask) { | |
cf01fb99 CS |
1638 | if (compat_get_bitmap(nodes_addr(bm), nmask, nr_bits)) |
1639 | return -EFAULT; | |
1da177e4 | 1640 | nm = compat_alloc_user_space(alloc_size); |
cf01fb99 CS |
1641 | if (copy_to_user(nm, nodes_addr(bm), alloc_size)) |
1642 | return -EFAULT; | |
1da177e4 LT |
1643 | } |
1644 | ||
1da177e4 LT |
1645 | return sys_mbind(start, len, mode, nm, nr_bits+1, flags); |
1646 | } | |
1647 | ||
1648 | #endif | |
1649 | ||
74d2c3a0 ON |
1650 | struct mempolicy *__get_vma_policy(struct vm_area_struct *vma, |
1651 | unsigned long addr) | |
1da177e4 | 1652 | { |
8d90274b | 1653 | struct mempolicy *pol = NULL; |
1da177e4 LT |
1654 | |
1655 | if (vma) { | |
480eccf9 | 1656 | if (vma->vm_ops && vma->vm_ops->get_policy) { |
8d90274b | 1657 | pol = vma->vm_ops->get_policy(vma, addr); |
00442ad0 | 1658 | } else if (vma->vm_policy) { |
1da177e4 | 1659 | pol = vma->vm_policy; |
00442ad0 MG |
1660 | |
1661 | /* | |
1662 | * shmem_alloc_page() passes MPOL_F_SHARED policy with | |
1663 | * a pseudo vma whose vma->vm_ops=NULL. Take a reference | |
1664 | * count on these policies which will be dropped by | |
1665 | * mpol_cond_put() later | |
1666 | */ | |
1667 | if (mpol_needs_cond_ref(pol)) | |
1668 | mpol_get(pol); | |
1669 | } | |
1da177e4 | 1670 | } |
f15ca78e | 1671 | |
74d2c3a0 ON |
1672 | return pol; |
1673 | } | |
1674 | ||
1675 | /* | |
dd6eecb9 | 1676 | * get_vma_policy(@vma, @addr) |
74d2c3a0 ON |
1677 | * @vma: virtual memory area whose policy is sought |
1678 | * @addr: address in @vma for shared policy lookup | |
1679 | * | |
1680 | * Returns effective policy for a VMA at specified address. | |
dd6eecb9 | 1681 | * Falls back to current->mempolicy or system default policy, as necessary. |
74d2c3a0 ON |
1682 | * Shared policies [those marked as MPOL_F_SHARED] require an extra reference |
1683 | * count--added by the get_policy() vm_op, as appropriate--to protect against | |
1684 | * freeing by another task. It is the caller's responsibility to free the | |
1685 | * extra reference for shared policies. | |
1686 | */ | |
dd6eecb9 ON |
1687 | static struct mempolicy *get_vma_policy(struct vm_area_struct *vma, |
1688 | unsigned long addr) | |
74d2c3a0 ON |
1689 | { |
1690 | struct mempolicy *pol = __get_vma_policy(vma, addr); | |
1691 | ||
8d90274b | 1692 | if (!pol) |
dd6eecb9 | 1693 | pol = get_task_policy(current); |
8d90274b | 1694 | |
1da177e4 LT |
1695 | return pol; |
1696 | } | |
1697 | ||
6b6482bb | 1698 | bool vma_policy_mof(struct vm_area_struct *vma) |
fc314724 | 1699 | { |
6b6482bb | 1700 | struct mempolicy *pol; |
fc314724 | 1701 | |
6b6482bb ON |
1702 | if (vma->vm_ops && vma->vm_ops->get_policy) { |
1703 | bool ret = false; | |
fc314724 | 1704 | |
6b6482bb ON |
1705 | pol = vma->vm_ops->get_policy(vma, vma->vm_start); |
1706 | if (pol && (pol->flags & MPOL_F_MOF)) | |
1707 | ret = true; | |
1708 | mpol_cond_put(pol); | |
8d90274b | 1709 | |
6b6482bb | 1710 | return ret; |
fc314724 MG |
1711 | } |
1712 | ||
6b6482bb | 1713 | pol = vma->vm_policy; |
8d90274b | 1714 | if (!pol) |
6b6482bb | 1715 | pol = get_task_policy(current); |
8d90274b | 1716 | |
fc314724 MG |
1717 | return pol->flags & MPOL_F_MOF; |
1718 | } | |
1719 | ||
d3eb1570 LJ |
1720 | static int apply_policy_zone(struct mempolicy *policy, enum zone_type zone) |
1721 | { | |
1722 | enum zone_type dynamic_policy_zone = policy_zone; | |
1723 | ||
1724 | BUG_ON(dynamic_policy_zone == ZONE_MOVABLE); | |
1725 | ||
1726 | /* | |
1727 | * if policy->v.nodes has movable memory only, | |
1728 | * we apply policy when gfp_zone(gfp) = ZONE_MOVABLE only. | |
1729 | * | |
1730 | * policy->v.nodes is intersect with node_states[N_MEMORY]. | |
1731 | * so if the following test faile, it implies | |
1732 | * policy->v.nodes has movable memory only. | |
1733 | */ | |
1734 | if (!nodes_intersects(policy->v.nodes, node_states[N_HIGH_MEMORY])) | |
1735 | dynamic_policy_zone = ZONE_MOVABLE; | |
1736 | ||
1737 | return zone >= dynamic_policy_zone; | |
1738 | } | |
1739 | ||
52cd3b07 LS |
1740 | /* |
1741 | * Return a nodemask representing a mempolicy for filtering nodes for | |
1742 | * page allocation | |
1743 | */ | |
1744 | static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy) | |
19770b32 MG |
1745 | { |
1746 | /* Lower zones don't get a nodemask applied for MPOL_BIND */ | |
45c4745a | 1747 | if (unlikely(policy->mode == MPOL_BIND) && |
d3eb1570 | 1748 | apply_policy_zone(policy, gfp_zone(gfp)) && |
19770b32 MG |
1749 | cpuset_nodemask_valid_mems_allowed(&policy->v.nodes)) |
1750 | return &policy->v.nodes; | |
1751 | ||
1752 | return NULL; | |
1753 | } | |
1754 | ||
04ec6264 VB |
1755 | /* Return the node id preferred by the given mempolicy, or the given id */ |
1756 | static int policy_node(gfp_t gfp, struct mempolicy *policy, | |
1757 | int nd) | |
1da177e4 | 1758 | { |
6d840958 MH |
1759 | if (policy->mode == MPOL_PREFERRED && !(policy->flags & MPOL_F_LOCAL)) |
1760 | nd = policy->v.preferred_node; | |
1761 | else { | |
19770b32 | 1762 | /* |
6d840958 MH |
1763 | * __GFP_THISNODE shouldn't even be used with the bind policy |
1764 | * because we might easily break the expectation to stay on the | |
1765 | * requested node and not break the policy. | |
19770b32 | 1766 | */ |
6d840958 | 1767 | WARN_ON_ONCE(policy->mode == MPOL_BIND && (gfp & __GFP_THISNODE)); |
1da177e4 | 1768 | } |
6d840958 | 1769 | |
04ec6264 | 1770 | return nd; |
1da177e4 LT |
1771 | } |
1772 | ||
1773 | /* Do dynamic interleaving for a process */ | |
1774 | static unsigned interleave_nodes(struct mempolicy *policy) | |
1775 | { | |
45816682 | 1776 | unsigned next; |
1da177e4 LT |
1777 | struct task_struct *me = current; |
1778 | ||
45816682 | 1779 | next = next_node_in(me->il_prev, policy->v.nodes); |
f5b087b5 | 1780 | if (next < MAX_NUMNODES) |
45816682 VB |
1781 | me->il_prev = next; |
1782 | return next; | |
1da177e4 LT |
1783 | } |
1784 | ||
dc85da15 CL |
1785 | /* |
1786 | * Depending on the memory policy provide a node from which to allocate the | |
1787 | * next slab entry. | |
1788 | */ | |
2a389610 | 1789 | unsigned int mempolicy_slab_node(void) |
dc85da15 | 1790 | { |
e7b691b0 | 1791 | struct mempolicy *policy; |
2a389610 | 1792 | int node = numa_mem_id(); |
e7b691b0 AK |
1793 | |
1794 | if (in_interrupt()) | |
2a389610 | 1795 | return node; |
e7b691b0 AK |
1796 | |
1797 | policy = current->mempolicy; | |
fc36b8d3 | 1798 | if (!policy || policy->flags & MPOL_F_LOCAL) |
2a389610 | 1799 | return node; |
bea904d5 LS |
1800 | |
1801 | switch (policy->mode) { | |
1802 | case MPOL_PREFERRED: | |
fc36b8d3 LS |
1803 | /* |
1804 | * handled MPOL_F_LOCAL above | |
1805 | */ | |
1806 | return policy->v.preferred_node; | |
765c4507 | 1807 | |
dc85da15 CL |
1808 | case MPOL_INTERLEAVE: |
1809 | return interleave_nodes(policy); | |
1810 | ||
dd1a239f | 1811 | case MPOL_BIND: { |
c33d6c06 MG |
1812 | struct zoneref *z; |
1813 | ||
dc85da15 CL |
1814 | /* |
1815 | * Follow bind policy behavior and start allocation at the | |
1816 | * first node. | |
1817 | */ | |
19770b32 | 1818 | struct zonelist *zonelist; |
19770b32 | 1819 | enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL); |
c9634cf0 | 1820 | zonelist = &NODE_DATA(node)->node_zonelists[ZONELIST_FALLBACK]; |
c33d6c06 MG |
1821 | z = first_zones_zonelist(zonelist, highest_zoneidx, |
1822 | &policy->v.nodes); | |
1823 | return z->zone ? z->zone->node : node; | |
dd1a239f | 1824 | } |
dc85da15 | 1825 | |
dc85da15 | 1826 | default: |
bea904d5 | 1827 | BUG(); |
dc85da15 CL |
1828 | } |
1829 | } | |
1830 | ||
fee83b3a AM |
1831 | /* |
1832 | * Do static interleaving for a VMA with known offset @n. Returns the n'th | |
1833 | * node in pol->v.nodes (starting from n=0), wrapping around if n exceeds the | |
1834 | * number of present nodes. | |
1835 | */ | |
98c70baa | 1836 | static unsigned offset_il_node(struct mempolicy *pol, unsigned long n) |
1da177e4 | 1837 | { |
dfcd3c0d | 1838 | unsigned nnodes = nodes_weight(pol->v.nodes); |
f5b087b5 | 1839 | unsigned target; |
fee83b3a AM |
1840 | int i; |
1841 | int nid; | |
1da177e4 | 1842 | |
f5b087b5 DR |
1843 | if (!nnodes) |
1844 | return numa_node_id(); | |
fee83b3a AM |
1845 | target = (unsigned int)n % nnodes; |
1846 | nid = first_node(pol->v.nodes); | |
1847 | for (i = 0; i < target; i++) | |
dfcd3c0d | 1848 | nid = next_node(nid, pol->v.nodes); |
1da177e4 LT |
1849 | return nid; |
1850 | } | |
1851 | ||
5da7ca86 CL |
1852 | /* Determine a node number for interleave */ |
1853 | static inline unsigned interleave_nid(struct mempolicy *pol, | |
1854 | struct vm_area_struct *vma, unsigned long addr, int shift) | |
1855 | { | |
1856 | if (vma) { | |
1857 | unsigned long off; | |
1858 | ||
3b98b087 NA |
1859 | /* |
1860 | * for small pages, there is no difference between | |
1861 | * shift and PAGE_SHIFT, so the bit-shift is safe. | |
1862 | * for huge pages, since vm_pgoff is in units of small | |
1863 | * pages, we need to shift off the always 0 bits to get | |
1864 | * a useful offset. | |
1865 | */ | |
1866 | BUG_ON(shift < PAGE_SHIFT); | |
1867 | off = vma->vm_pgoff >> (shift - PAGE_SHIFT); | |
5da7ca86 | 1868 | off += (addr - vma->vm_start) >> shift; |
98c70baa | 1869 | return offset_il_node(pol, off); |
5da7ca86 CL |
1870 | } else |
1871 | return interleave_nodes(pol); | |
1872 | } | |
1873 | ||
00ac59ad | 1874 | #ifdef CONFIG_HUGETLBFS |
480eccf9 | 1875 | /* |
04ec6264 | 1876 | * huge_node(@vma, @addr, @gfp_flags, @mpol) |
b46e14ac FF |
1877 | * @vma: virtual memory area whose policy is sought |
1878 | * @addr: address in @vma for shared policy lookup and interleave policy | |
1879 | * @gfp_flags: for requested zone | |
1880 | * @mpol: pointer to mempolicy pointer for reference counted mempolicy | |
1881 | * @nodemask: pointer to nodemask pointer for MPOL_BIND nodemask | |
480eccf9 | 1882 | * |
04ec6264 | 1883 | * Returns a nid suitable for a huge page allocation and a pointer |
52cd3b07 LS |
1884 | * to the struct mempolicy for conditional unref after allocation. |
1885 | * If the effective policy is 'BIND, returns a pointer to the mempolicy's | |
1886 | * @nodemask for filtering the zonelist. | |
c0ff7453 | 1887 | * |
d26914d1 | 1888 | * Must be protected by read_mems_allowed_begin() |
480eccf9 | 1889 | */ |
04ec6264 VB |
1890 | int huge_node(struct vm_area_struct *vma, unsigned long addr, gfp_t gfp_flags, |
1891 | struct mempolicy **mpol, nodemask_t **nodemask) | |
5da7ca86 | 1892 | { |
04ec6264 | 1893 | int nid; |
5da7ca86 | 1894 | |
dd6eecb9 | 1895 | *mpol = get_vma_policy(vma, addr); |
19770b32 | 1896 | *nodemask = NULL; /* assume !MPOL_BIND */ |
5da7ca86 | 1897 | |
52cd3b07 | 1898 | if (unlikely((*mpol)->mode == MPOL_INTERLEAVE)) { |
04ec6264 VB |
1899 | nid = interleave_nid(*mpol, vma, addr, |
1900 | huge_page_shift(hstate_vma(vma))); | |
52cd3b07 | 1901 | } else { |
04ec6264 | 1902 | nid = policy_node(gfp_flags, *mpol, numa_node_id()); |
52cd3b07 LS |
1903 | if ((*mpol)->mode == MPOL_BIND) |
1904 | *nodemask = &(*mpol)->v.nodes; | |
480eccf9 | 1905 | } |
04ec6264 | 1906 | return nid; |
5da7ca86 | 1907 | } |
06808b08 LS |
1908 | |
1909 | /* | |
1910 | * init_nodemask_of_mempolicy | |
1911 | * | |
1912 | * If the current task's mempolicy is "default" [NULL], return 'false' | |
1913 | * to indicate default policy. Otherwise, extract the policy nodemask | |
1914 | * for 'bind' or 'interleave' policy into the argument nodemask, or | |
1915 | * initialize the argument nodemask to contain the single node for | |
1916 | * 'preferred' or 'local' policy and return 'true' to indicate presence | |
1917 | * of non-default mempolicy. | |
1918 | * | |
1919 | * We don't bother with reference counting the mempolicy [mpol_get/put] | |
1920 | * because the current task is examining it's own mempolicy and a task's | |
1921 | * mempolicy is only ever changed by the task itself. | |
1922 | * | |
1923 | * N.B., it is the caller's responsibility to free a returned nodemask. | |
1924 | */ | |
1925 | bool init_nodemask_of_mempolicy(nodemask_t *mask) | |
1926 | { | |
1927 | struct mempolicy *mempolicy; | |
1928 | int nid; | |
1929 | ||
1930 | if (!(mask && current->mempolicy)) | |
1931 | return false; | |
1932 | ||
c0ff7453 | 1933 | task_lock(current); |
06808b08 LS |
1934 | mempolicy = current->mempolicy; |
1935 | switch (mempolicy->mode) { | |
1936 | case MPOL_PREFERRED: | |
1937 | if (mempolicy->flags & MPOL_F_LOCAL) | |
1938 | nid = numa_node_id(); | |
1939 | else | |
1940 | nid = mempolicy->v.preferred_node; | |
1941 | init_nodemask_of_node(mask, nid); | |
1942 | break; | |
1943 | ||
1944 | case MPOL_BIND: | |
1945 | /* Fall through */ | |
1946 | case MPOL_INTERLEAVE: | |
1947 | *mask = mempolicy->v.nodes; | |
1948 | break; | |
1949 | ||
1950 | default: | |
1951 | BUG(); | |
1952 | } | |
c0ff7453 | 1953 | task_unlock(current); |
06808b08 LS |
1954 | |
1955 | return true; | |
1956 | } | |
00ac59ad | 1957 | #endif |
5da7ca86 | 1958 | |
6f48d0eb DR |
1959 | /* |
1960 | * mempolicy_nodemask_intersects | |
1961 | * | |
1962 | * If tsk's mempolicy is "default" [NULL], return 'true' to indicate default | |
1963 | * policy. Otherwise, check for intersection between mask and the policy | |
1964 | * nodemask for 'bind' or 'interleave' policy. For 'perferred' or 'local' | |
1965 | * policy, always return true since it may allocate elsewhere on fallback. | |
1966 | * | |
1967 | * Takes task_lock(tsk) to prevent freeing of its mempolicy. | |
1968 | */ | |
1969 | bool mempolicy_nodemask_intersects(struct task_struct *tsk, | |
1970 | const nodemask_t *mask) | |
1971 | { | |
1972 | struct mempolicy *mempolicy; | |
1973 | bool ret = true; | |
1974 | ||
1975 | if (!mask) | |
1976 | return ret; | |
1977 | task_lock(tsk); | |
1978 | mempolicy = tsk->mempolicy; | |
1979 | if (!mempolicy) | |
1980 | goto out; | |
1981 | ||
1982 | switch (mempolicy->mode) { | |
1983 | case MPOL_PREFERRED: | |
1984 | /* | |
1985 | * MPOL_PREFERRED and MPOL_F_LOCAL are only preferred nodes to | |
1986 | * allocate from, they may fallback to other nodes when oom. | |
1987 | * Thus, it's possible for tsk to have allocated memory from | |
1988 | * nodes in mask. | |
1989 | */ | |
1990 | break; | |
1991 | case MPOL_BIND: | |
1992 | case MPOL_INTERLEAVE: | |
1993 | ret = nodes_intersects(mempolicy->v.nodes, *mask); | |
1994 | break; | |
1995 | default: | |
1996 | BUG(); | |
1997 | } | |
1998 | out: | |
1999 | task_unlock(tsk); | |
2000 | return ret; | |
2001 | } | |
2002 | ||
1da177e4 LT |
2003 | /* Allocate a page in interleaved policy. |
2004 | Own path because it needs to do special accounting. */ | |
662f3a0b AK |
2005 | static struct page *alloc_page_interleave(gfp_t gfp, unsigned order, |
2006 | unsigned nid) | |
1da177e4 | 2007 | { |
1da177e4 LT |
2008 | struct page *page; |
2009 | ||
04ec6264 | 2010 | page = __alloc_pages(gfp, order, nid); |
4518085e KW |
2011 | /* skip NUMA_INTERLEAVE_HIT counter update if numa stats is disabled */ |
2012 | if (!static_branch_likely(&vm_numa_stat_key)) | |
2013 | return page; | |
de55c8b2 AR |
2014 | if (page && page_to_nid(page) == nid) { |
2015 | preempt_disable(); | |
2016 | __inc_numa_state(page_zone(page), NUMA_INTERLEAVE_HIT); | |
2017 | preempt_enable(); | |
2018 | } | |
1da177e4 LT |
2019 | return page; |
2020 | } | |
2021 | ||
2022 | /** | |
0bbbc0b3 | 2023 | * alloc_pages_vma - Allocate a page for a VMA. |
1da177e4 LT |
2024 | * |
2025 | * @gfp: | |
2026 | * %GFP_USER user allocation. | |
2027 | * %GFP_KERNEL kernel allocations, | |
2028 | * %GFP_HIGHMEM highmem/user allocations, | |
2029 | * %GFP_FS allocation should not call back into a file system. | |
2030 | * %GFP_ATOMIC don't sleep. | |
2031 | * | |
0bbbc0b3 | 2032 | * @order:Order of the GFP allocation. |
1da177e4 LT |
2033 | * @vma: Pointer to VMA or NULL if not available. |
2034 | * @addr: Virtual Address of the allocation. Must be inside the VMA. | |
be97a41b VB |
2035 | * @node: Which node to prefer for allocation (modulo policy). |
2036 | * @hugepage: for hugepages try only the preferred node if possible | |
1da177e4 LT |
2037 | * |
2038 | * This function allocates a page from the kernel page pool and applies | |
2039 | * a NUMA policy associated with the VMA or the current process. | |
2040 | * When VMA is not NULL caller must hold down_read on the mmap_sem of the | |
2041 | * mm_struct of the VMA to prevent it from going away. Should be used for | |
be97a41b VB |
2042 | * all allocations for pages that will be mapped into user space. Returns |
2043 | * NULL when no page can be allocated. | |
1da177e4 LT |
2044 | */ |
2045 | struct page * | |
0bbbc0b3 | 2046 | alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma, |
be97a41b | 2047 | unsigned long addr, int node, bool hugepage) |
1da177e4 | 2048 | { |
cc9a6c87 | 2049 | struct mempolicy *pol; |
c0ff7453 | 2050 | struct page *page; |
04ec6264 | 2051 | int preferred_nid; |
be97a41b | 2052 | nodemask_t *nmask; |
cc9a6c87 | 2053 | |
dd6eecb9 | 2054 | pol = get_vma_policy(vma, addr); |
1da177e4 | 2055 | |
0867a57c VB |
2056 | if (pol->mode == MPOL_INTERLEAVE) { |
2057 | unsigned nid; | |
2058 | ||
2059 | nid = interleave_nid(pol, vma, addr, PAGE_SHIFT + order); | |
2060 | mpol_cond_put(pol); | |
2061 | page = alloc_page_interleave(gfp, order, nid); | |
2062 | goto out; | |
2063 | } | |
2064 | ||
2065 | if (unlikely(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hugepage)) { | |
2066 | int hpage_node = node; | |
2067 | ||
be97a41b VB |
2068 | /* |
2069 | * For hugepage allocation and non-interleave policy which | |
0867a57c VB |
2070 | * allows the current node (or other explicitly preferred |
2071 | * node) we only try to allocate from the current/preferred | |
2072 | * node and don't fall back to other nodes, as the cost of | |
2073 | * remote accesses would likely offset THP benefits. | |
be97a41b VB |
2074 | * |
2075 | * If the policy is interleave, or does not allow the current | |
2076 | * node in its nodemask, we allocate the standard way. | |
2077 | */ | |
0867a57c VB |
2078 | if (pol->mode == MPOL_PREFERRED && |
2079 | !(pol->flags & MPOL_F_LOCAL)) | |
2080 | hpage_node = pol->v.preferred_node; | |
2081 | ||
be97a41b | 2082 | nmask = policy_nodemask(gfp, pol); |
0867a57c | 2083 | if (!nmask || node_isset(hpage_node, *nmask)) { |
be97a41b | 2084 | mpol_cond_put(pol); |
e16f31f4 AA |
2085 | /* |
2086 | * We cannot invoke reclaim if __GFP_THISNODE | |
2087 | * is set. Invoking reclaim with | |
2088 | * __GFP_THISNODE set, would cause THP | |
2089 | * allocations to trigger heavy swapping | |
2090 | * despite there may be tons of free memory | |
2091 | * (including potentially plenty of THP | |
2092 | * already available in the buddy) on all the | |
2093 | * other NUMA nodes. | |
2094 | * | |
2095 | * At most we could invoke compaction when | |
2096 | * __GFP_THISNODE is set (but we would need to | |
2097 | * refrain from invoking reclaim even if | |
2098 | * compaction returned COMPACT_SKIPPED because | |
2099 | * there wasn't not enough memory to succeed | |
2100 | * compaction). For now just avoid | |
2101 | * __GFP_THISNODE instead of limiting the | |
2102 | * allocation path to a strict and single | |
2103 | * compaction invocation. | |
2104 | * | |
2105 | * Supposedly if direct reclaim was enabled by | |
2106 | * the caller, the app prefers THP regardless | |
2107 | * of the node it comes from so this would be | |
2108 | * more desiderable behavior than only | |
2109 | * providing THP originated from the local | |
2110 | * node in such case. | |
2111 | */ | |
2112 | if (!(gfp & __GFP_DIRECT_RECLAIM)) | |
2113 | gfp |= __GFP_THISNODE; | |
2114 | page = __alloc_pages_node(hpage_node, gfp, order); | |
be97a41b VB |
2115 | goto out; |
2116 | } | |
2117 | } | |
2118 | ||
be97a41b | 2119 | nmask = policy_nodemask(gfp, pol); |
04ec6264 VB |
2120 | preferred_nid = policy_node(gfp, pol, node); |
2121 | page = __alloc_pages_nodemask(gfp, order, preferred_nid, nmask); | |
d51e9894 | 2122 | mpol_cond_put(pol); |
be97a41b | 2123 | out: |
c0ff7453 | 2124 | return page; |
1da177e4 LT |
2125 | } |
2126 | ||
2127 | /** | |
2128 | * alloc_pages_current - Allocate pages. | |
2129 | * | |
2130 | * @gfp: | |
2131 | * %GFP_USER user allocation, | |
2132 | * %GFP_KERNEL kernel allocation, | |
2133 | * %GFP_HIGHMEM highmem allocation, | |
2134 | * %GFP_FS don't call back into a file system. | |
2135 | * %GFP_ATOMIC don't sleep. | |
2136 | * @order: Power of two of allocation size in pages. 0 is a single page. | |
2137 | * | |
2138 | * Allocate a page from the kernel page pool. When not in | |
2139 | * interrupt context and apply the current process NUMA policy. | |
2140 | * Returns NULL when no page can be allocated. | |
1da177e4 | 2141 | */ |
dd0fc66f | 2142 | struct page *alloc_pages_current(gfp_t gfp, unsigned order) |
1da177e4 | 2143 | { |
8d90274b | 2144 | struct mempolicy *pol = &default_policy; |
c0ff7453 | 2145 | struct page *page; |
1da177e4 | 2146 | |
8d90274b ON |
2147 | if (!in_interrupt() && !(gfp & __GFP_THISNODE)) |
2148 | pol = get_task_policy(current); | |
52cd3b07 LS |
2149 | |
2150 | /* | |
2151 | * No reference counting needed for current->mempolicy | |
2152 | * nor system default_policy | |
2153 | */ | |
45c4745a | 2154 | if (pol->mode == MPOL_INTERLEAVE) |
c0ff7453 MX |
2155 | page = alloc_page_interleave(gfp, order, interleave_nodes(pol)); |
2156 | else | |
2157 | page = __alloc_pages_nodemask(gfp, order, | |
04ec6264 | 2158 | policy_node(gfp, pol, numa_node_id()), |
5c4b4be3 | 2159 | policy_nodemask(gfp, pol)); |
cc9a6c87 | 2160 | |
c0ff7453 | 2161 | return page; |
1da177e4 LT |
2162 | } |
2163 | EXPORT_SYMBOL(alloc_pages_current); | |
2164 | ||
ef0855d3 ON |
2165 | int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst) |
2166 | { | |
2167 | struct mempolicy *pol = mpol_dup(vma_policy(src)); | |
2168 | ||
2169 | if (IS_ERR(pol)) | |
2170 | return PTR_ERR(pol); | |
2171 | dst->vm_policy = pol; | |
2172 | return 0; | |
2173 | } | |
2174 | ||
4225399a | 2175 | /* |
846a16bf | 2176 | * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it |
4225399a PJ |
2177 | * rebinds the mempolicy its copying by calling mpol_rebind_policy() |
2178 | * with the mems_allowed returned by cpuset_mems_allowed(). This | |
2179 | * keeps mempolicies cpuset relative after its cpuset moves. See | |
2180 | * further kernel/cpuset.c update_nodemask(). | |
708c1bbc MX |
2181 | * |
2182 | * current's mempolicy may be rebinded by the other task(the task that changes | |
2183 | * cpuset's mems), so we needn't do rebind work for current task. | |
4225399a | 2184 | */ |
4225399a | 2185 | |
846a16bf LS |
2186 | /* Slow path of a mempolicy duplicate */ |
2187 | struct mempolicy *__mpol_dup(struct mempolicy *old) | |
1da177e4 LT |
2188 | { |
2189 | struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL); | |
2190 | ||
2191 | if (!new) | |
2192 | return ERR_PTR(-ENOMEM); | |
708c1bbc MX |
2193 | |
2194 | /* task's mempolicy is protected by alloc_lock */ | |
2195 | if (old == current->mempolicy) { | |
2196 | task_lock(current); | |
2197 | *new = *old; | |
2198 | task_unlock(current); | |
2199 | } else | |
2200 | *new = *old; | |
2201 | ||
4225399a PJ |
2202 | if (current_cpuset_is_being_rebound()) { |
2203 | nodemask_t mems = cpuset_mems_allowed(current); | |
213980c0 | 2204 | mpol_rebind_policy(new, &mems); |
4225399a | 2205 | } |
1da177e4 | 2206 | atomic_set(&new->refcnt, 1); |
1da177e4 LT |
2207 | return new; |
2208 | } | |
2209 | ||
2210 | /* Slow path of a mempolicy comparison */ | |
fcfb4dcc | 2211 | bool __mpol_equal(struct mempolicy *a, struct mempolicy *b) |
1da177e4 LT |
2212 | { |
2213 | if (!a || !b) | |
fcfb4dcc | 2214 | return false; |
45c4745a | 2215 | if (a->mode != b->mode) |
fcfb4dcc | 2216 | return false; |
19800502 | 2217 | if (a->flags != b->flags) |
fcfb4dcc | 2218 | return false; |
19800502 BL |
2219 | if (mpol_store_user_nodemask(a)) |
2220 | if (!nodes_equal(a->w.user_nodemask, b->w.user_nodemask)) | |
fcfb4dcc | 2221 | return false; |
19800502 | 2222 | |
45c4745a | 2223 | switch (a->mode) { |
19770b32 MG |
2224 | case MPOL_BIND: |
2225 | /* Fall through */ | |
1da177e4 | 2226 | case MPOL_INTERLEAVE: |
fcfb4dcc | 2227 | return !!nodes_equal(a->v.nodes, b->v.nodes); |
1da177e4 | 2228 | case MPOL_PREFERRED: |
bcb76352 YX |
2229 | /* a's ->flags is the same as b's */ |
2230 | if (a->flags & MPOL_F_LOCAL) | |
2231 | return true; | |
75719661 | 2232 | return a->v.preferred_node == b->v.preferred_node; |
1da177e4 LT |
2233 | default: |
2234 | BUG(); | |
fcfb4dcc | 2235 | return false; |
1da177e4 LT |
2236 | } |
2237 | } | |
2238 | ||
1da177e4 LT |
2239 | /* |
2240 | * Shared memory backing store policy support. | |
2241 | * | |
2242 | * Remember policies even when nobody has shared memory mapped. | |
2243 | * The policies are kept in Red-Black tree linked from the inode. | |
4a8c7bb5 | 2244 | * They are protected by the sp->lock rwlock, which should be held |
1da177e4 LT |
2245 | * for any accesses to the tree. |
2246 | */ | |
2247 | ||
4a8c7bb5 NZ |
2248 | /* |
2249 | * lookup first element intersecting start-end. Caller holds sp->lock for | |
2250 | * reading or for writing | |
2251 | */ | |
1da177e4 LT |
2252 | static struct sp_node * |
2253 | sp_lookup(struct shared_policy *sp, unsigned long start, unsigned long end) | |
2254 | { | |
2255 | struct rb_node *n = sp->root.rb_node; | |
2256 | ||
2257 | while (n) { | |
2258 | struct sp_node *p = rb_entry(n, struct sp_node, nd); | |
2259 | ||
2260 | if (start >= p->end) | |
2261 | n = n->rb_right; | |
2262 | else if (end <= p->start) | |
2263 | n = n->rb_left; | |
2264 | else | |
2265 | break; | |
2266 | } | |
2267 | if (!n) | |
2268 | return NULL; | |
2269 | for (;;) { | |
2270 | struct sp_node *w = NULL; | |
2271 | struct rb_node *prev = rb_prev(n); | |
2272 | if (!prev) | |
2273 | break; | |
2274 | w = rb_entry(prev, struct sp_node, nd); | |
2275 | if (w->end <= start) | |
2276 | break; | |
2277 | n = prev; | |
2278 | } | |
2279 | return rb_entry(n, struct sp_node, nd); | |
2280 | } | |
2281 | ||
4a8c7bb5 NZ |
2282 | /* |
2283 | * Insert a new shared policy into the list. Caller holds sp->lock for | |
2284 | * writing. | |
2285 | */ | |
1da177e4 LT |
2286 | static void sp_insert(struct shared_policy *sp, struct sp_node *new) |
2287 | { | |
2288 | struct rb_node **p = &sp->root.rb_node; | |
2289 | struct rb_node *parent = NULL; | |
2290 | struct sp_node *nd; | |
2291 | ||
2292 | while (*p) { | |
2293 | parent = *p; | |
2294 | nd = rb_entry(parent, struct sp_node, nd); | |
2295 | if (new->start < nd->start) | |
2296 | p = &(*p)->rb_left; | |
2297 | else if (new->end > nd->end) | |
2298 | p = &(*p)->rb_right; | |
2299 | else | |
2300 | BUG(); | |
2301 | } | |
2302 | rb_link_node(&new->nd, parent, p); | |
2303 | rb_insert_color(&new->nd, &sp->root); | |
140d5a49 | 2304 | pr_debug("inserting %lx-%lx: %d\n", new->start, new->end, |
45c4745a | 2305 | new->policy ? new->policy->mode : 0); |
1da177e4 LT |
2306 | } |
2307 | ||
2308 | /* Find shared policy intersecting idx */ | |
2309 | struct mempolicy * | |
2310 | mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx) | |
2311 | { | |
2312 | struct mempolicy *pol = NULL; | |
2313 | struct sp_node *sn; | |
2314 | ||
2315 | if (!sp->root.rb_node) | |
2316 | return NULL; | |
4a8c7bb5 | 2317 | read_lock(&sp->lock); |
1da177e4 LT |
2318 | sn = sp_lookup(sp, idx, idx+1); |
2319 | if (sn) { | |
2320 | mpol_get(sn->policy); | |
2321 | pol = sn->policy; | |
2322 | } | |
4a8c7bb5 | 2323 | read_unlock(&sp->lock); |
1da177e4 LT |
2324 | return pol; |
2325 | } | |
2326 | ||
63f74ca2 KM |
2327 | static void sp_free(struct sp_node *n) |
2328 | { | |
2329 | mpol_put(n->policy); | |
2330 | kmem_cache_free(sn_cache, n); | |
2331 | } | |
2332 | ||
771fb4d8 LS |
2333 | /** |
2334 | * mpol_misplaced - check whether current page node is valid in policy | |
2335 | * | |
b46e14ac FF |
2336 | * @page: page to be checked |
2337 | * @vma: vm area where page mapped | |
2338 | * @addr: virtual address where page mapped | |
771fb4d8 LS |
2339 | * |
2340 | * Lookup current policy node id for vma,addr and "compare to" page's | |
2341 | * node id. | |
2342 | * | |
2343 | * Returns: | |
2344 | * -1 - not misplaced, page is in the right node | |
2345 | * node - node id where the page should be | |
2346 | * | |
2347 | * Policy determination "mimics" alloc_page_vma(). | |
2348 | * Called from fault path where we know the vma and faulting address. | |
2349 | */ | |
2350 | int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned long addr) | |
2351 | { | |
2352 | struct mempolicy *pol; | |
c33d6c06 | 2353 | struct zoneref *z; |
771fb4d8 LS |
2354 | int curnid = page_to_nid(page); |
2355 | unsigned long pgoff; | |
90572890 PZ |
2356 | int thiscpu = raw_smp_processor_id(); |
2357 | int thisnid = cpu_to_node(thiscpu); | |
771fb4d8 LS |
2358 | int polnid = -1; |
2359 | int ret = -1; | |
2360 | ||
dd6eecb9 | 2361 | pol = get_vma_policy(vma, addr); |
771fb4d8 LS |
2362 | if (!(pol->flags & MPOL_F_MOF)) |
2363 | goto out; | |
2364 | ||
2365 | switch (pol->mode) { | |
2366 | case MPOL_INTERLEAVE: | |
771fb4d8 LS |
2367 | pgoff = vma->vm_pgoff; |
2368 | pgoff += (addr - vma->vm_start) >> PAGE_SHIFT; | |
98c70baa | 2369 | polnid = offset_il_node(pol, pgoff); |
771fb4d8 LS |
2370 | break; |
2371 | ||
2372 | case MPOL_PREFERRED: | |
2373 | if (pol->flags & MPOL_F_LOCAL) | |
2374 | polnid = numa_node_id(); | |
2375 | else | |
2376 | polnid = pol->v.preferred_node; | |
2377 | break; | |
2378 | ||
2379 | case MPOL_BIND: | |
c33d6c06 | 2380 | |
771fb4d8 LS |
2381 | /* |
2382 | * allows binding to multiple nodes. | |
2383 | * use current page if in policy nodemask, | |
2384 | * else select nearest allowed node, if any. | |
2385 | * If no allowed nodes, use current [!misplaced]. | |
2386 | */ | |
2387 | if (node_isset(curnid, pol->v.nodes)) | |
2388 | goto out; | |
c33d6c06 | 2389 | z = first_zones_zonelist( |
771fb4d8 LS |
2390 | node_zonelist(numa_node_id(), GFP_HIGHUSER), |
2391 | gfp_zone(GFP_HIGHUSER), | |
c33d6c06 MG |
2392 | &pol->v.nodes); |
2393 | polnid = z->zone->node; | |
771fb4d8 LS |
2394 | break; |
2395 | ||
2396 | default: | |
2397 | BUG(); | |
2398 | } | |
5606e387 MG |
2399 | |
2400 | /* Migrate the page towards the node whose CPU is referencing it */ | |
e42c8ff2 | 2401 | if (pol->flags & MPOL_F_MORON) { |
90572890 | 2402 | polnid = thisnid; |
5606e387 | 2403 | |
10f39042 | 2404 | if (!should_numa_migrate_memory(current, page, curnid, thiscpu)) |
de1c9ce6 | 2405 | goto out; |
e42c8ff2 MG |
2406 | } |
2407 | ||
771fb4d8 LS |
2408 | if (curnid != polnid) |
2409 | ret = polnid; | |
2410 | out: | |
2411 | mpol_cond_put(pol); | |
2412 | ||
2413 | return ret; | |
2414 | } | |
2415 | ||
c11600e4 DR |
2416 | /* |
2417 | * Drop the (possibly final) reference to task->mempolicy. It needs to be | |
2418 | * dropped after task->mempolicy is set to NULL so that any allocation done as | |
2419 | * part of its kmem_cache_free(), such as by KASAN, doesn't reference a freed | |
2420 | * policy. | |
2421 | */ | |
2422 | void mpol_put_task_policy(struct task_struct *task) | |
2423 | { | |
2424 | struct mempolicy *pol; | |
2425 | ||
2426 | task_lock(task); | |
2427 | pol = task->mempolicy; | |
2428 | task->mempolicy = NULL; | |
2429 | task_unlock(task); | |
2430 | mpol_put(pol); | |
2431 | } | |
2432 | ||
1da177e4 LT |
2433 | static void sp_delete(struct shared_policy *sp, struct sp_node *n) |
2434 | { | |
140d5a49 | 2435 | pr_debug("deleting %lx-l%lx\n", n->start, n->end); |
1da177e4 | 2436 | rb_erase(&n->nd, &sp->root); |
63f74ca2 | 2437 | sp_free(n); |
1da177e4 LT |
2438 | } |
2439 | ||
42288fe3 MG |
2440 | static void sp_node_init(struct sp_node *node, unsigned long start, |
2441 | unsigned long end, struct mempolicy *pol) | |
2442 | { | |
2443 | node->start = start; | |
2444 | node->end = end; | |
2445 | node->policy = pol; | |
2446 | } | |
2447 | ||
dbcb0f19 AB |
2448 | static struct sp_node *sp_alloc(unsigned long start, unsigned long end, |
2449 | struct mempolicy *pol) | |
1da177e4 | 2450 | { |
869833f2 KM |
2451 | struct sp_node *n; |
2452 | struct mempolicy *newpol; | |
1da177e4 | 2453 | |
869833f2 | 2454 | n = kmem_cache_alloc(sn_cache, GFP_KERNEL); |
1da177e4 LT |
2455 | if (!n) |
2456 | return NULL; | |
869833f2 KM |
2457 | |
2458 | newpol = mpol_dup(pol); | |
2459 | if (IS_ERR(newpol)) { | |
2460 | kmem_cache_free(sn_cache, n); | |
2461 | return NULL; | |
2462 | } | |
2463 | newpol->flags |= MPOL_F_SHARED; | |
42288fe3 | 2464 | sp_node_init(n, start, end, newpol); |
869833f2 | 2465 | |
1da177e4 LT |
2466 | return n; |
2467 | } | |
2468 | ||
2469 | /* Replace a policy range. */ | |
2470 | static int shared_policy_replace(struct shared_policy *sp, unsigned long start, | |
2471 | unsigned long end, struct sp_node *new) | |
2472 | { | |
b22d127a | 2473 | struct sp_node *n; |
42288fe3 MG |
2474 | struct sp_node *n_new = NULL; |
2475 | struct mempolicy *mpol_new = NULL; | |
b22d127a | 2476 | int ret = 0; |
1da177e4 | 2477 | |
42288fe3 | 2478 | restart: |
4a8c7bb5 | 2479 | write_lock(&sp->lock); |
1da177e4 LT |
2480 | n = sp_lookup(sp, start, end); |
2481 | /* Take care of old policies in the same range. */ | |
2482 | while (n && n->start < end) { | |
2483 | struct rb_node *next = rb_next(&n->nd); | |
2484 | if (n->start >= start) { | |
2485 | if (n->end <= end) | |
2486 | sp_delete(sp, n); | |
2487 | else | |
2488 | n->start = end; | |
2489 | } else { | |
2490 | /* Old policy spanning whole new range. */ | |
2491 | if (n->end > end) { | |
42288fe3 MG |
2492 | if (!n_new) |
2493 | goto alloc_new; | |
2494 | ||
2495 | *mpol_new = *n->policy; | |
2496 | atomic_set(&mpol_new->refcnt, 1); | |
7880639c | 2497 | sp_node_init(n_new, end, n->end, mpol_new); |
1da177e4 | 2498 | n->end = start; |
5ca39575 | 2499 | sp_insert(sp, n_new); |
42288fe3 MG |
2500 | n_new = NULL; |
2501 | mpol_new = NULL; | |
1da177e4 LT |
2502 | break; |
2503 | } else | |
2504 | n->end = start; | |
2505 | } | |
2506 | if (!next) | |
2507 | break; | |
2508 | n = rb_entry(next, struct sp_node, nd); | |
2509 | } | |
2510 | if (new) | |
2511 | sp_insert(sp, new); | |
4a8c7bb5 | 2512 | write_unlock(&sp->lock); |
42288fe3 MG |
2513 | ret = 0; |
2514 | ||
2515 | err_out: | |
2516 | if (mpol_new) | |
2517 | mpol_put(mpol_new); | |
2518 | if (n_new) | |
2519 | kmem_cache_free(sn_cache, n_new); | |
2520 | ||
b22d127a | 2521 | return ret; |
42288fe3 MG |
2522 | |
2523 | alloc_new: | |
4a8c7bb5 | 2524 | write_unlock(&sp->lock); |
42288fe3 MG |
2525 | ret = -ENOMEM; |
2526 | n_new = kmem_cache_alloc(sn_cache, GFP_KERNEL); | |
2527 | if (!n_new) | |
2528 | goto err_out; | |
2529 | mpol_new = kmem_cache_alloc(policy_cache, GFP_KERNEL); | |
2530 | if (!mpol_new) | |
2531 | goto err_out; | |
2532 | goto restart; | |
1da177e4 LT |
2533 | } |
2534 | ||
71fe804b LS |
2535 | /** |
2536 | * mpol_shared_policy_init - initialize shared policy for inode | |
2537 | * @sp: pointer to inode shared policy | |
2538 | * @mpol: struct mempolicy to install | |
2539 | * | |
2540 | * Install non-NULL @mpol in inode's shared policy rb-tree. | |
2541 | * On entry, the current task has a reference on a non-NULL @mpol. | |
2542 | * This must be released on exit. | |
4bfc4495 | 2543 | * This is called at get_inode() calls and we can use GFP_KERNEL. |
71fe804b LS |
2544 | */ |
2545 | void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol) | |
2546 | { | |
58568d2a MX |
2547 | int ret; |
2548 | ||
71fe804b | 2549 | sp->root = RB_ROOT; /* empty tree == default mempolicy */ |
4a8c7bb5 | 2550 | rwlock_init(&sp->lock); |
71fe804b LS |
2551 | |
2552 | if (mpol) { | |
2553 | struct vm_area_struct pvma; | |
2554 | struct mempolicy *new; | |
4bfc4495 | 2555 | NODEMASK_SCRATCH(scratch); |
71fe804b | 2556 | |
4bfc4495 | 2557 | if (!scratch) |
5c0c1654 | 2558 | goto put_mpol; |
71fe804b LS |
2559 | /* contextualize the tmpfs mount point mempolicy */ |
2560 | new = mpol_new(mpol->mode, mpol->flags, &mpol->w.user_nodemask); | |
15d77835 | 2561 | if (IS_ERR(new)) |
0cae3457 | 2562 | goto free_scratch; /* no valid nodemask intersection */ |
58568d2a MX |
2563 | |
2564 | task_lock(current); | |
4bfc4495 | 2565 | ret = mpol_set_nodemask(new, &mpol->w.user_nodemask, scratch); |
58568d2a | 2566 | task_unlock(current); |
15d77835 | 2567 | if (ret) |
5c0c1654 | 2568 | goto put_new; |
71fe804b LS |
2569 | |
2570 | /* Create pseudo-vma that contains just the policy */ | |
2571 | memset(&pvma, 0, sizeof(struct vm_area_struct)); | |
2572 | pvma.vm_end = TASK_SIZE; /* policy covers entire file */ | |
2573 | mpol_set_shared_policy(sp, &pvma, new); /* adds ref */ | |
15d77835 | 2574 | |
5c0c1654 | 2575 | put_new: |
71fe804b | 2576 | mpol_put(new); /* drop initial ref */ |
0cae3457 | 2577 | free_scratch: |
4bfc4495 | 2578 | NODEMASK_SCRATCH_FREE(scratch); |
5c0c1654 LS |
2579 | put_mpol: |
2580 | mpol_put(mpol); /* drop our incoming ref on sb mpol */ | |
7339ff83 RH |
2581 | } |
2582 | } | |
2583 | ||
1da177e4 LT |
2584 | int mpol_set_shared_policy(struct shared_policy *info, |
2585 | struct vm_area_struct *vma, struct mempolicy *npol) | |
2586 | { | |
2587 | int err; | |
2588 | struct sp_node *new = NULL; | |
2589 | unsigned long sz = vma_pages(vma); | |
2590 | ||
028fec41 | 2591 | pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n", |
1da177e4 | 2592 | vma->vm_pgoff, |
45c4745a | 2593 | sz, npol ? npol->mode : -1, |
028fec41 | 2594 | npol ? npol->flags : -1, |
00ef2d2f | 2595 | npol ? nodes_addr(npol->v.nodes)[0] : NUMA_NO_NODE); |
1da177e4 LT |
2596 | |
2597 | if (npol) { | |
2598 | new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol); | |
2599 | if (!new) | |
2600 | return -ENOMEM; | |
2601 | } | |
2602 | err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new); | |
2603 | if (err && new) | |
63f74ca2 | 2604 | sp_free(new); |
1da177e4 LT |
2605 | return err; |
2606 | } | |
2607 | ||
2608 | /* Free a backing policy store on inode delete. */ | |
2609 | void mpol_free_shared_policy(struct shared_policy *p) | |
2610 | { | |
2611 | struct sp_node *n; | |
2612 | struct rb_node *next; | |
2613 | ||
2614 | if (!p->root.rb_node) | |
2615 | return; | |
4a8c7bb5 | 2616 | write_lock(&p->lock); |
1da177e4 LT |
2617 | next = rb_first(&p->root); |
2618 | while (next) { | |
2619 | n = rb_entry(next, struct sp_node, nd); | |
2620 | next = rb_next(&n->nd); | |
63f74ca2 | 2621 | sp_delete(p, n); |
1da177e4 | 2622 | } |
4a8c7bb5 | 2623 | write_unlock(&p->lock); |
1da177e4 LT |
2624 | } |
2625 | ||
1a687c2e | 2626 | #ifdef CONFIG_NUMA_BALANCING |
c297663c | 2627 | static int __initdata numabalancing_override; |
1a687c2e MG |
2628 | |
2629 | static void __init check_numabalancing_enable(void) | |
2630 | { | |
2631 | bool numabalancing_default = false; | |
2632 | ||
2633 | if (IS_ENABLED(CONFIG_NUMA_BALANCING_DEFAULT_ENABLED)) | |
2634 | numabalancing_default = true; | |
2635 | ||
c297663c MG |
2636 | /* Parsed by setup_numabalancing. override == 1 enables, -1 disables */ |
2637 | if (numabalancing_override) | |
2638 | set_numabalancing_state(numabalancing_override == 1); | |
2639 | ||
b0dc2b9b | 2640 | if (num_online_nodes() > 1 && !numabalancing_override) { |
756a025f | 2641 | pr_info("%s automatic NUMA balancing. Configure with numa_balancing= or the kernel.numa_balancing sysctl\n", |
c297663c | 2642 | numabalancing_default ? "Enabling" : "Disabling"); |
1a687c2e MG |
2643 | set_numabalancing_state(numabalancing_default); |
2644 | } | |
2645 | } | |
2646 | ||
2647 | static int __init setup_numabalancing(char *str) | |
2648 | { | |
2649 | int ret = 0; | |
2650 | if (!str) | |
2651 | goto out; | |
1a687c2e MG |
2652 | |
2653 | if (!strcmp(str, "enable")) { | |
c297663c | 2654 | numabalancing_override = 1; |
1a687c2e MG |
2655 | ret = 1; |
2656 | } else if (!strcmp(str, "disable")) { | |
c297663c | 2657 | numabalancing_override = -1; |
1a687c2e MG |
2658 | ret = 1; |
2659 | } | |
2660 | out: | |
2661 | if (!ret) | |
4a404bea | 2662 | pr_warn("Unable to parse numa_balancing=\n"); |
1a687c2e MG |
2663 | |
2664 | return ret; | |
2665 | } | |
2666 | __setup("numa_balancing=", setup_numabalancing); | |
2667 | #else | |
2668 | static inline void __init check_numabalancing_enable(void) | |
2669 | { | |
2670 | } | |
2671 | #endif /* CONFIG_NUMA_BALANCING */ | |
2672 | ||
1da177e4 LT |
2673 | /* assumes fs == KERNEL_DS */ |
2674 | void __init numa_policy_init(void) | |
2675 | { | |
b71636e2 PM |
2676 | nodemask_t interleave_nodes; |
2677 | unsigned long largest = 0; | |
2678 | int nid, prefer = 0; | |
2679 | ||
1da177e4 LT |
2680 | policy_cache = kmem_cache_create("numa_policy", |
2681 | sizeof(struct mempolicy), | |
20c2df83 | 2682 | 0, SLAB_PANIC, NULL); |
1da177e4 LT |
2683 | |
2684 | sn_cache = kmem_cache_create("shared_policy_node", | |
2685 | sizeof(struct sp_node), | |
20c2df83 | 2686 | 0, SLAB_PANIC, NULL); |
1da177e4 | 2687 | |
5606e387 MG |
2688 | for_each_node(nid) { |
2689 | preferred_node_policy[nid] = (struct mempolicy) { | |
2690 | .refcnt = ATOMIC_INIT(1), | |
2691 | .mode = MPOL_PREFERRED, | |
2692 | .flags = MPOL_F_MOF | MPOL_F_MORON, | |
2693 | .v = { .preferred_node = nid, }, | |
2694 | }; | |
2695 | } | |
2696 | ||
b71636e2 PM |
2697 | /* |
2698 | * Set interleaving policy for system init. Interleaving is only | |
2699 | * enabled across suitably sized nodes (default is >= 16MB), or | |
2700 | * fall back to the largest node if they're all smaller. | |
2701 | */ | |
2702 | nodes_clear(interleave_nodes); | |
01f13bd6 | 2703 | for_each_node_state(nid, N_MEMORY) { |
b71636e2 PM |
2704 | unsigned long total_pages = node_present_pages(nid); |
2705 | ||
2706 | /* Preserve the largest node */ | |
2707 | if (largest < total_pages) { | |
2708 | largest = total_pages; | |
2709 | prefer = nid; | |
2710 | } | |
2711 | ||
2712 | /* Interleave this node? */ | |
2713 | if ((total_pages << PAGE_SHIFT) >= (16 << 20)) | |
2714 | node_set(nid, interleave_nodes); | |
2715 | } | |
2716 | ||
2717 | /* All too small, use the largest */ | |
2718 | if (unlikely(nodes_empty(interleave_nodes))) | |
2719 | node_set(prefer, interleave_nodes); | |
1da177e4 | 2720 | |
028fec41 | 2721 | if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes)) |
b1de0d13 | 2722 | pr_err("%s: interleaving failed\n", __func__); |
1a687c2e MG |
2723 | |
2724 | check_numabalancing_enable(); | |
1da177e4 LT |
2725 | } |
2726 | ||
8bccd85f | 2727 | /* Reset policy of current process to default */ |
1da177e4 LT |
2728 | void numa_default_policy(void) |
2729 | { | |
028fec41 | 2730 | do_set_mempolicy(MPOL_DEFAULT, 0, NULL); |
1da177e4 | 2731 | } |
68860ec1 | 2732 | |
095f1fc4 LS |
2733 | /* |
2734 | * Parse and format mempolicy from/to strings | |
2735 | */ | |
2736 | ||
1a75a6c8 | 2737 | /* |
f2a07f40 | 2738 | * "local" is implemented internally by MPOL_PREFERRED with MPOL_F_LOCAL flag. |
1a75a6c8 | 2739 | */ |
345ace9c LS |
2740 | static const char * const policy_modes[] = |
2741 | { | |
2742 | [MPOL_DEFAULT] = "default", | |
2743 | [MPOL_PREFERRED] = "prefer", | |
2744 | [MPOL_BIND] = "bind", | |
2745 | [MPOL_INTERLEAVE] = "interleave", | |
d3a71033 | 2746 | [MPOL_LOCAL] = "local", |
345ace9c | 2747 | }; |
1a75a6c8 | 2748 | |
095f1fc4 LS |
2749 | |
2750 | #ifdef CONFIG_TMPFS | |
2751 | /** | |
f2a07f40 | 2752 | * mpol_parse_str - parse string to mempolicy, for tmpfs mpol mount option. |
095f1fc4 | 2753 | * @str: string containing mempolicy to parse |
71fe804b | 2754 | * @mpol: pointer to struct mempolicy pointer, returned on success. |
095f1fc4 LS |
2755 | * |
2756 | * Format of input: | |
2757 | * <mode>[=<flags>][:<nodelist>] | |
2758 | * | |
71fe804b | 2759 | * On success, returns 0, else 1 |
095f1fc4 | 2760 | */ |
a7a88b23 | 2761 | int mpol_parse_str(char *str, struct mempolicy **mpol) |
095f1fc4 | 2762 | { |
71fe804b | 2763 | struct mempolicy *new = NULL; |
b4652e84 | 2764 | unsigned short mode; |
f2a07f40 | 2765 | unsigned short mode_flags; |
71fe804b | 2766 | nodemask_t nodes; |
095f1fc4 LS |
2767 | char *nodelist = strchr(str, ':'); |
2768 | char *flags = strchr(str, '='); | |
095f1fc4 LS |
2769 | int err = 1; |
2770 | ||
2771 | if (nodelist) { | |
2772 | /* NUL-terminate mode or flags string */ | |
2773 | *nodelist++ = '\0'; | |
71fe804b | 2774 | if (nodelist_parse(nodelist, nodes)) |
095f1fc4 | 2775 | goto out; |
01f13bd6 | 2776 | if (!nodes_subset(nodes, node_states[N_MEMORY])) |
095f1fc4 | 2777 | goto out; |
71fe804b LS |
2778 | } else |
2779 | nodes_clear(nodes); | |
2780 | ||
095f1fc4 LS |
2781 | if (flags) |
2782 | *flags++ = '\0'; /* terminate mode string */ | |
2783 | ||
479e2802 | 2784 | for (mode = 0; mode < MPOL_MAX; mode++) { |
345ace9c | 2785 | if (!strcmp(str, policy_modes[mode])) { |
095f1fc4 LS |
2786 | break; |
2787 | } | |
2788 | } | |
a720094d | 2789 | if (mode >= MPOL_MAX) |
095f1fc4 LS |
2790 | goto out; |
2791 | ||
71fe804b | 2792 | switch (mode) { |
095f1fc4 | 2793 | case MPOL_PREFERRED: |
71fe804b LS |
2794 | /* |
2795 | * Insist on a nodelist of one node only | |
2796 | */ | |
095f1fc4 LS |
2797 | if (nodelist) { |
2798 | char *rest = nodelist; | |
2799 | while (isdigit(*rest)) | |
2800 | rest++; | |
926f2ae0 KM |
2801 | if (*rest) |
2802 | goto out; | |
095f1fc4 LS |
2803 | } |
2804 | break; | |
095f1fc4 LS |
2805 | case MPOL_INTERLEAVE: |
2806 | /* | |
2807 | * Default to online nodes with memory if no nodelist | |
2808 | */ | |
2809 | if (!nodelist) | |
01f13bd6 | 2810 | nodes = node_states[N_MEMORY]; |
3f226aa1 | 2811 | break; |
71fe804b | 2812 | case MPOL_LOCAL: |
3f226aa1 | 2813 | /* |
71fe804b | 2814 | * Don't allow a nodelist; mpol_new() checks flags |
3f226aa1 | 2815 | */ |
71fe804b | 2816 | if (nodelist) |
3f226aa1 | 2817 | goto out; |
71fe804b | 2818 | mode = MPOL_PREFERRED; |
3f226aa1 | 2819 | break; |
413b43de RT |
2820 | case MPOL_DEFAULT: |
2821 | /* | |
2822 | * Insist on a empty nodelist | |
2823 | */ | |
2824 | if (!nodelist) | |
2825 | err = 0; | |
2826 | goto out; | |
d69b2e63 KM |
2827 | case MPOL_BIND: |
2828 | /* | |
2829 | * Insist on a nodelist | |
2830 | */ | |
2831 | if (!nodelist) | |
2832 | goto out; | |
095f1fc4 LS |
2833 | } |
2834 | ||
71fe804b | 2835 | mode_flags = 0; |
095f1fc4 LS |
2836 | if (flags) { |
2837 | /* | |
2838 | * Currently, we only support two mutually exclusive | |
2839 | * mode flags. | |
2840 | */ | |
2841 | if (!strcmp(flags, "static")) | |
71fe804b | 2842 | mode_flags |= MPOL_F_STATIC_NODES; |
095f1fc4 | 2843 | else if (!strcmp(flags, "relative")) |
71fe804b | 2844 | mode_flags |= MPOL_F_RELATIVE_NODES; |
095f1fc4 | 2845 | else |
926f2ae0 | 2846 | goto out; |
095f1fc4 | 2847 | } |
71fe804b LS |
2848 | |
2849 | new = mpol_new(mode, mode_flags, &nodes); | |
2850 | if (IS_ERR(new)) | |
926f2ae0 KM |
2851 | goto out; |
2852 | ||
f2a07f40 HD |
2853 | /* |
2854 | * Save nodes for mpol_to_str() to show the tmpfs mount options | |
2855 | * for /proc/mounts, /proc/pid/mounts and /proc/pid/mountinfo. | |
2856 | */ | |
2857 | if (mode != MPOL_PREFERRED) | |
2858 | new->v.nodes = nodes; | |
2859 | else if (nodelist) | |
2860 | new->v.preferred_node = first_node(nodes); | |
2861 | else | |
2862 | new->flags |= MPOL_F_LOCAL; | |
2863 | ||
2864 | /* | |
2865 | * Save nodes for contextualization: this will be used to "clone" | |
2866 | * the mempolicy in a specific context [cpuset] at a later time. | |
2867 | */ | |
2868 | new->w.user_nodemask = nodes; | |
2869 | ||
926f2ae0 | 2870 | err = 0; |
71fe804b | 2871 | |
095f1fc4 LS |
2872 | out: |
2873 | /* Restore string for error message */ | |
2874 | if (nodelist) | |
2875 | *--nodelist = ':'; | |
2876 | if (flags) | |
2877 | *--flags = '='; | |
71fe804b LS |
2878 | if (!err) |
2879 | *mpol = new; | |
095f1fc4 LS |
2880 | return err; |
2881 | } | |
2882 | #endif /* CONFIG_TMPFS */ | |
2883 | ||
71fe804b LS |
2884 | /** |
2885 | * mpol_to_str - format a mempolicy structure for printing | |
2886 | * @buffer: to contain formatted mempolicy string | |
2887 | * @maxlen: length of @buffer | |
2888 | * @pol: pointer to mempolicy to be formatted | |
71fe804b | 2889 | * |
948927ee DR |
2890 | * Convert @pol into a string. If @buffer is too short, truncate the string. |
2891 | * Recommend a @maxlen of at least 32 for the longest mode, "interleave", the | |
2892 | * longest flag, "relative", and to display at least a few node ids. | |
1a75a6c8 | 2893 | */ |
948927ee | 2894 | void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) |
1a75a6c8 CL |
2895 | { |
2896 | char *p = buffer; | |
948927ee DR |
2897 | nodemask_t nodes = NODE_MASK_NONE; |
2898 | unsigned short mode = MPOL_DEFAULT; | |
2899 | unsigned short flags = 0; | |
2291990a | 2900 | |
8790c71a | 2901 | if (pol && pol != &default_policy && !(pol->flags & MPOL_F_MORON)) { |
bea904d5 | 2902 | mode = pol->mode; |
948927ee DR |
2903 | flags = pol->flags; |
2904 | } | |
bea904d5 | 2905 | |
1a75a6c8 CL |
2906 | switch (mode) { |
2907 | case MPOL_DEFAULT: | |
1a75a6c8 | 2908 | break; |
1a75a6c8 | 2909 | case MPOL_PREFERRED: |
fc36b8d3 | 2910 | if (flags & MPOL_F_LOCAL) |
f2a07f40 | 2911 | mode = MPOL_LOCAL; |
53f2556b | 2912 | else |
fc36b8d3 | 2913 | node_set(pol->v.preferred_node, nodes); |
1a75a6c8 | 2914 | break; |
1a75a6c8 | 2915 | case MPOL_BIND: |
1a75a6c8 | 2916 | case MPOL_INTERLEAVE: |
f2a07f40 | 2917 | nodes = pol->v.nodes; |
1a75a6c8 | 2918 | break; |
1a75a6c8 | 2919 | default: |
948927ee DR |
2920 | WARN_ON_ONCE(1); |
2921 | snprintf(p, maxlen, "unknown"); | |
2922 | return; | |
1a75a6c8 CL |
2923 | } |
2924 | ||
b7a9f420 | 2925 | p += snprintf(p, maxlen, "%s", policy_modes[mode]); |
1a75a6c8 | 2926 | |
fc36b8d3 | 2927 | if (flags & MPOL_MODE_FLAGS) { |
948927ee | 2928 | p += snprintf(p, buffer + maxlen - p, "="); |
f5b087b5 | 2929 | |
2291990a LS |
2930 | /* |
2931 | * Currently, the only defined flags are mutually exclusive | |
2932 | */ | |
f5b087b5 | 2933 | if (flags & MPOL_F_STATIC_NODES) |
2291990a LS |
2934 | p += snprintf(p, buffer + maxlen - p, "static"); |
2935 | else if (flags & MPOL_F_RELATIVE_NODES) | |
2936 | p += snprintf(p, buffer + maxlen - p, "relative"); | |
f5b087b5 DR |
2937 | } |
2938 | ||
9e763e0f TH |
2939 | if (!nodes_empty(nodes)) |
2940 | p += scnprintf(p, buffer + maxlen - p, ":%*pbl", | |
2941 | nodemask_pr_args(&nodes)); | |
1a75a6c8 | 2942 | } |