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