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 | * | |
1cd1a4e7 | 11 | * Support six policies per VMA and per process: |
1da177e4 LT |
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 | * |
fa3bea4e GP |
22 | * weighted interleave |
23 | * Allocate memory interleaved over a set of nodes based on | |
24 | * a set of weights (per-node), with normal fallback if it | |
25 | * fails. Otherwise operates the same as interleave. | |
26 | * Example: nodeset(0,1) & weights (2,1) - 2 pages allocated | |
27 | * on node 0 for every 1 page allocated on node 1. | |
28 | * | |
1da177e4 LT |
29 | * bind Only allocate memory on a specific set of nodes, |
30 | * no fallback. | |
8bccd85f CL |
31 | * FIXME: memory is allocated starting with the first node |
32 | * to the last. It would be better if bind would truly restrict | |
33 | * the allocation to memory nodes instead | |
34 | * | |
c36f6e6d | 35 | * preferred Try a specific node first before normal fallback. |
00ef2d2f | 36 | * As a special case NUMA_NO_NODE here means do the allocation |
1da177e4 LT |
37 | * on the local CPU. This is normally identical to default, |
38 | * but useful to set in a VMA when you have a non default | |
39 | * process policy. | |
8bccd85f | 40 | * |
b27abacc DH |
41 | * preferred many Try a set of nodes first before normal fallback. This is |
42 | * similar to preferred without the special case. | |
43 | * | |
1da177e4 LT |
44 | * default Allocate on the local node first, or when on a VMA |
45 | * use the process policy. This is what Linux always did | |
46 | * in a NUMA aware kernel and still does by, ahem, default. | |
47 | * | |
48 | * The process policy is applied for most non interrupt memory allocations | |
49 | * in that process' context. Interrupts ignore the policies and always | |
50 | * try to allocate on the local CPU. The VMA policy is only applied for memory | |
51 | * allocations for a VMA in the VM. | |
52 | * | |
53 | * Currently there are a few corner cases in swapping where the policy | |
54 | * is not applied, but the majority should be handled. When process policy | |
55 | * is used it is not remembered over swap outs/swap ins. | |
56 | * | |
57 | * Only the highest zone in the zone hierarchy gets policied. Allocations | |
58 | * requesting a lower zone just use default policy. This implies that | |
59 | * on systems with highmem kernel lowmem allocation don't get policied. | |
60 | * Same with GFP_DMA allocations. | |
61 | * | |
c36f6e6d | 62 | * For shmem/tmpfs shared memory the policy is shared between |
1da177e4 LT |
63 | * all users and remembered even when nobody has memory mapped. |
64 | */ | |
65 | ||
66 | /* Notebook: | |
67 | fix mmap readahead to honour policy and enable policy for any page cache | |
68 | object | |
69 | statistics for bigpages | |
70 | global policy for page cache? currently it uses process policy. Requires | |
71 | first item above. | |
72 | handle mremap for shared memory (currently ignored for the policy) | |
73 | grows down? | |
74 | make bind policy root only? It can trigger oom much faster and the | |
75 | kernel is not always grateful with that. | |
1da177e4 LT |
76 | */ |
77 | ||
b1de0d13 MH |
78 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
79 | ||
1da177e4 | 80 | #include <linux/mempolicy.h> |
a520110e | 81 | #include <linux/pagewalk.h> |
1da177e4 LT |
82 | #include <linux/highmem.h> |
83 | #include <linux/hugetlb.h> | |
84 | #include <linux/kernel.h> | |
85 | #include <linux/sched.h> | |
6e84f315 | 86 | #include <linux/sched/mm.h> |
6a3827d7 | 87 | #include <linux/sched/numa_balancing.h> |
f719ff9b | 88 | #include <linux/sched/task.h> |
1da177e4 LT |
89 | #include <linux/nodemask.h> |
90 | #include <linux/cpuset.h> | |
1da177e4 LT |
91 | #include <linux/slab.h> |
92 | #include <linux/string.h> | |
b95f1b31 | 93 | #include <linux/export.h> |
b488893a | 94 | #include <linux/nsproxy.h> |
1da177e4 LT |
95 | #include <linux/interrupt.h> |
96 | #include <linux/init.h> | |
97 | #include <linux/compat.h> | |
31367466 | 98 | #include <linux/ptrace.h> |
dc9aa5b9 | 99 | #include <linux/swap.h> |
1a75a6c8 CL |
100 | #include <linux/seq_file.h> |
101 | #include <linux/proc_fs.h> | |
b20a3503 | 102 | #include <linux/migrate.h> |
62b61f61 | 103 | #include <linux/ksm.h> |
95a402c3 | 104 | #include <linux/rmap.h> |
86c3a764 | 105 | #include <linux/security.h> |
dbcb0f19 | 106 | #include <linux/syscalls.h> |
095f1fc4 | 107 | #include <linux/ctype.h> |
6d9c285a | 108 | #include <linux/mm_inline.h> |
b24f53a0 | 109 | #include <linux/mmu_notifier.h> |
b1de0d13 | 110 | #include <linux/printk.h> |
c8633798 | 111 | #include <linux/swapops.h> |
e341f9c3 | 112 | #include <linux/gcd.h> |
dc9aa5b9 | 113 | |
1da177e4 | 114 | #include <asm/tlbflush.h> |
4a18419f | 115 | #include <asm/tlb.h> |
7c0f6ba6 | 116 | #include <linux/uaccess.h> |
dec92bf9 | 117 | #include <linux/memory.h> |
1da177e4 | 118 | |
62695a84 NP |
119 | #include "internal.h" |
120 | ||
38e35860 | 121 | /* Internal flags */ |
dc9aa5b9 | 122 | #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for continuous vmas */ |
1cb5d11a HD |
123 | #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for nodemask */ |
124 | #define MPOL_MF_WRLOCK (MPOL_MF_INTERNAL << 2) /* Write-lock walked vmas */ | |
dc9aa5b9 | 125 | |
fcc234f8 PE |
126 | static struct kmem_cache *policy_cache; |
127 | static struct kmem_cache *sn_cache; | |
1da177e4 | 128 | |
1da177e4 LT |
129 | /* Highest zone. An specific allocation for a zone below that is not |
130 | policied. */ | |
6267276f | 131 | enum zone_type policy_zone = 0; |
1da177e4 | 132 | |
bea904d5 LS |
133 | /* |
134 | * run-time system-wide default policy => local allocation | |
135 | */ | |
e754d79d | 136 | static struct mempolicy default_policy = { |
1da177e4 | 137 | .refcnt = ATOMIC_INIT(1), /* never free it */ |
7858d7bc | 138 | .mode = MPOL_LOCAL, |
1da177e4 LT |
139 | }; |
140 | ||
5606e387 MG |
141 | static struct mempolicy preferred_node_policy[MAX_NUMNODES]; |
142 | ||
dce41f5a | 143 | /* |
e341f9c3 JH |
144 | * weightiness balances the tradeoff between small weights (cycles through nodes |
145 | * faster, more fair/even distribution) and large weights (smaller errors | |
146 | * between actual bandwidth ratios and weight ratios). 32 is a number that has | |
147 | * been found to perform at a reasonable compromise between the two goals. | |
148 | */ | |
149 | static const int weightiness = 32; | |
150 | ||
151 | /* | |
152 | * A null weighted_interleave_state is interpreted as having .mode="auto", | |
153 | * and .iw_table is interpreted as an array of 1s with length nr_node_ids. | |
154 | */ | |
155 | struct weighted_interleave_state { | |
156 | bool mode_auto; | |
157 | u8 iw_table[]; | |
158 | }; | |
159 | static struct weighted_interleave_state __rcu *wi_state; | |
160 | static unsigned int *node_bw_table; | |
161 | ||
162 | /* | |
163 | * wi_state_lock protects both wi_state and node_bw_table. | |
164 | * node_bw_table is only used by writers to update wi_state. | |
dce41f5a | 165 | */ |
e341f9c3 | 166 | static DEFINE_MUTEX(wi_state_lock); |
dce41f5a RK |
167 | |
168 | static u8 get_il_weight(int node) | |
169 | { | |
e341f9c3 JH |
170 | struct weighted_interleave_state *state; |
171 | u8 weight = 1; | |
dce41f5a RK |
172 | |
173 | rcu_read_lock(); | |
e341f9c3 JH |
174 | state = rcu_dereference(wi_state); |
175 | if (state) | |
176 | weight = state->iw_table[node]; | |
dce41f5a RK |
177 | rcu_read_unlock(); |
178 | return weight; | |
179 | } | |
180 | ||
e341f9c3 JH |
181 | /* |
182 | * Convert bandwidth values into weighted interleave weights. | |
183 | * Call with wi_state_lock. | |
184 | */ | |
185 | static void reduce_interleave_weights(unsigned int *bw, u8 *new_iw) | |
186 | { | |
187 | u64 sum_bw = 0; | |
188 | unsigned int cast_sum_bw, scaling_factor = 1, iw_gcd = 0; | |
189 | int nid; | |
190 | ||
191 | for_each_node_state(nid, N_MEMORY) | |
192 | sum_bw += bw[nid]; | |
193 | ||
194 | /* Scale bandwidths to whole numbers in the range [1, weightiness] */ | |
195 | for_each_node_state(nid, N_MEMORY) { | |
196 | /* | |
197 | * Try not to perform 64-bit division. | |
198 | * If sum_bw < scaling_factor, then sum_bw < U32_MAX. | |
199 | * If sum_bw > scaling_factor, then round the weight up to 1. | |
200 | */ | |
201 | scaling_factor = weightiness * bw[nid]; | |
202 | if (bw[nid] && sum_bw < scaling_factor) { | |
203 | cast_sum_bw = (unsigned int)sum_bw; | |
204 | new_iw[nid] = scaling_factor / cast_sum_bw; | |
205 | } else { | |
206 | new_iw[nid] = 1; | |
207 | } | |
208 | if (!iw_gcd) | |
209 | iw_gcd = new_iw[nid]; | |
210 | iw_gcd = gcd(iw_gcd, new_iw[nid]); | |
211 | } | |
212 | ||
213 | /* 1:2 is strictly better than 16:32. Reduce by the weights' GCD. */ | |
214 | for_each_node_state(nid, N_MEMORY) | |
215 | new_iw[nid] /= iw_gcd; | |
216 | } | |
217 | ||
218 | int mempolicy_set_node_perf(unsigned int node, struct access_coordinate *coords) | |
219 | { | |
220 | struct weighted_interleave_state *new_wi_state, *old_wi_state = NULL; | |
221 | unsigned int *old_bw, *new_bw; | |
222 | unsigned int bw_val; | |
223 | int i; | |
224 | ||
225 | bw_val = min(coords->read_bandwidth, coords->write_bandwidth); | |
226 | new_bw = kcalloc(nr_node_ids, sizeof(unsigned int), GFP_KERNEL); | |
227 | if (!new_bw) | |
228 | return -ENOMEM; | |
229 | ||
230 | new_wi_state = kmalloc(struct_size(new_wi_state, iw_table, nr_node_ids), | |
231 | GFP_KERNEL); | |
232 | if (!new_wi_state) { | |
233 | kfree(new_bw); | |
234 | return -ENOMEM; | |
235 | } | |
236 | new_wi_state->mode_auto = true; | |
237 | for (i = 0; i < nr_node_ids; i++) | |
238 | new_wi_state->iw_table[i] = 1; | |
239 | ||
240 | /* | |
241 | * Update bandwidth info, even in manual mode. That way, when switching | |
242 | * to auto mode in the future, iw_table can be overwritten using | |
243 | * accurate bw data. | |
244 | */ | |
245 | mutex_lock(&wi_state_lock); | |
246 | ||
247 | old_bw = node_bw_table; | |
248 | if (old_bw) | |
249 | memcpy(new_bw, old_bw, nr_node_ids * sizeof(*old_bw)); | |
250 | new_bw[node] = bw_val; | |
251 | node_bw_table = new_bw; | |
252 | ||
253 | old_wi_state = rcu_dereference_protected(wi_state, | |
254 | lockdep_is_held(&wi_state_lock)); | |
255 | if (old_wi_state && !old_wi_state->mode_auto) { | |
256 | /* Manual mode; skip reducing weights and updating wi_state */ | |
257 | mutex_unlock(&wi_state_lock); | |
258 | kfree(new_wi_state); | |
259 | goto out; | |
260 | } | |
261 | ||
262 | /* NULL wi_state assumes auto=true; reduce weights and update wi_state*/ | |
263 | reduce_interleave_weights(new_bw, new_wi_state->iw_table); | |
264 | rcu_assign_pointer(wi_state, new_wi_state); | |
265 | ||
266 | mutex_unlock(&wi_state_lock); | |
267 | if (old_wi_state) { | |
268 | synchronize_rcu(); | |
269 | kfree(old_wi_state); | |
270 | } | |
271 | out: | |
272 | kfree(old_bw); | |
273 | return 0; | |
274 | } | |
275 | ||
b2ca916c | 276 | /** |
b1f099b1 | 277 | * numa_nearest_node - Find nearest node by state |
f6e92f40 | 278 | * @node: Node id to start the search |
b1f099b1 | 279 | * @state: State to filter the search |
b2ca916c | 280 | * |
b1f099b1 | 281 | * Lookup the closest node by distance if @nid is not in state. |
dad5b023 | 282 | * |
b1f099b1 | 283 | * Return: this @node if it is in state, otherwise the closest node by distance |
b2ca916c | 284 | */ |
b1f099b1 | 285 | int numa_nearest_node(int node, unsigned int state) |
b2ca916c | 286 | { |
4fcbe96e | 287 | int min_dist = INT_MAX, dist, n, min_node; |
b2ca916c | 288 | |
b1f099b1 YN |
289 | if (state >= NR_NODE_STATES) |
290 | return -EINVAL; | |
291 | ||
292 | if (node == NUMA_NO_NODE || node_state(node, state)) | |
4fcbe96e | 293 | return node; |
b2ca916c DW |
294 | |
295 | min_node = node; | |
b1f099b1 | 296 | for_each_node_state(n, state) { |
4fcbe96e DW |
297 | dist = node_distance(node, n); |
298 | if (dist < min_dist) { | |
299 | min_dist = dist; | |
300 | min_node = n; | |
b2ca916c DW |
301 | } |
302 | } | |
303 | ||
304 | return min_node; | |
305 | } | |
b1f099b1 | 306 | EXPORT_SYMBOL_GPL(numa_nearest_node); |
b2ca916c | 307 | |
16d79f2a AR |
308 | /** |
309 | * nearest_node_nodemask - Find the node in @mask at the nearest distance | |
310 | * from @node. | |
311 | * | |
312 | * @node: a valid node ID to start the search from. | |
313 | * @mask: a pointer to a nodemask representing the allowed nodes. | |
314 | * | |
315 | * This function iterates over all nodes in @mask and calculates the | |
316 | * distance from the starting @node, then it returns the node ID that is | |
317 | * the closest to @node, or MAX_NUMNODES if no node is found. | |
318 | * | |
319 | * Note that @node must be a valid node ID usable with node_distance(), | |
320 | * providing an invalid node ID (e.g., NUMA_NO_NODE) may result in crashes | |
321 | * or unexpected behavior. | |
322 | */ | |
323 | int nearest_node_nodemask(int node, nodemask_t *mask) | |
324 | { | |
325 | int dist, n, min_dist = INT_MAX, min_node = MAX_NUMNODES; | |
326 | ||
327 | for_each_node_mask(n, *mask) { | |
328 | dist = node_distance(node, n); | |
329 | if (dist < min_dist) { | |
330 | min_dist = dist; | |
331 | min_node = n; | |
332 | } | |
333 | } | |
334 | ||
335 | return min_node; | |
336 | } | |
337 | EXPORT_SYMBOL_GPL(nearest_node_nodemask); | |
338 | ||
74d2c3a0 | 339 | struct mempolicy *get_task_policy(struct task_struct *p) |
5606e387 MG |
340 | { |
341 | struct mempolicy *pol = p->mempolicy; | |
f15ca78e | 342 | int node; |
5606e387 | 343 | |
f15ca78e ON |
344 | if (pol) |
345 | return pol; | |
5606e387 | 346 | |
f15ca78e ON |
347 | node = numa_node_id(); |
348 | if (node != NUMA_NO_NODE) { | |
349 | pol = &preferred_node_policy[node]; | |
350 | /* preferred_node_policy is not initialised early in boot */ | |
351 | if (pol->mode) | |
352 | return pol; | |
5606e387 MG |
353 | } |
354 | ||
f15ca78e | 355 | return &default_policy; |
5606e387 MG |
356 | } |
357 | ||
37012946 DR |
358 | static const struct mempolicy_operations { |
359 | int (*create)(struct mempolicy *pol, const nodemask_t *nodes); | |
213980c0 | 360 | void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes); |
37012946 DR |
361 | } mpol_ops[MPOL_MAX]; |
362 | ||
f5b087b5 DR |
363 | static inline int mpol_store_user_nodemask(const struct mempolicy *pol) |
364 | { | |
6d556294 | 365 | return pol->flags & MPOL_MODE_FLAGS; |
4c50bc01 DR |
366 | } |
367 | ||
368 | static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig, | |
369 | const nodemask_t *rel) | |
370 | { | |
371 | nodemask_t tmp; | |
372 | nodes_fold(tmp, *orig, nodes_weight(*rel)); | |
373 | nodes_onto(*ret, tmp, *rel); | |
f5b087b5 DR |
374 | } |
375 | ||
be897d48 | 376 | static int mpol_new_nodemask(struct mempolicy *pol, const nodemask_t *nodes) |
37012946 DR |
377 | { |
378 | if (nodes_empty(*nodes)) | |
379 | return -EINVAL; | |
269fbe72 | 380 | pol->nodes = *nodes; |
37012946 DR |
381 | return 0; |
382 | } | |
383 | ||
384 | static int mpol_new_preferred(struct mempolicy *pol, const nodemask_t *nodes) | |
385 | { | |
7858d7bc FT |
386 | if (nodes_empty(*nodes)) |
387 | return -EINVAL; | |
269fbe72 BW |
388 | |
389 | nodes_clear(pol->nodes); | |
390 | node_set(first_node(*nodes), pol->nodes); | |
37012946 DR |
391 | return 0; |
392 | } | |
393 | ||
58568d2a MX |
394 | /* |
395 | * mpol_set_nodemask is called after mpol_new() to set up the nodemask, if | |
396 | * any, for the new policy. mpol_new() has already validated the nodes | |
7858d7bc | 397 | * parameter with respect to the policy mode and flags. |
58568d2a MX |
398 | * |
399 | * Must be called holding task's alloc_lock to protect task's mems_allowed | |
c1e8d7c6 | 400 | * and mempolicy. May also be called holding the mmap_lock for write. |
58568d2a | 401 | */ |
4bfc4495 KH |
402 | static int mpol_set_nodemask(struct mempolicy *pol, |
403 | const nodemask_t *nodes, struct nodemask_scratch *nsc) | |
58568d2a | 404 | { |
58568d2a MX |
405 | int ret; |
406 | ||
7858d7bc FT |
407 | /* |
408 | * Default (pol==NULL) resp. local memory policies are not a | |
409 | * subject of any remapping. They also do not need any special | |
410 | * constructor. | |
411 | */ | |
412 | if (!pol || pol->mode == MPOL_LOCAL) | |
58568d2a | 413 | return 0; |
7858d7bc | 414 | |
01f13bd6 | 415 | /* Check N_MEMORY */ |
4bfc4495 | 416 | nodes_and(nsc->mask1, |
01f13bd6 | 417 | cpuset_current_mems_allowed, node_states[N_MEMORY]); |
58568d2a MX |
418 | |
419 | VM_BUG_ON(!nodes); | |
4bfc4495 | 420 | |
7858d7bc FT |
421 | if (pol->flags & MPOL_F_RELATIVE_NODES) |
422 | mpol_relative_nodemask(&nsc->mask2, nodes, &nsc->mask1); | |
423 | else | |
424 | nodes_and(nsc->mask2, *nodes, nsc->mask1); | |
58568d2a | 425 | |
7858d7bc FT |
426 | if (mpol_store_user_nodemask(pol)) |
427 | pol->w.user_nodemask = *nodes; | |
4bfc4495 | 428 | else |
7858d7bc FT |
429 | pol->w.cpuset_mems_allowed = cpuset_current_mems_allowed; |
430 | ||
431 | ret = mpol_ops[pol->mode].create(pol, &nsc->mask2); | |
58568d2a MX |
432 | return ret; |
433 | } | |
434 | ||
435 | /* | |
436 | * This function just creates a new policy, does some check and simple | |
437 | * initialization. You must invoke mpol_set_nodemask() to set nodes. | |
438 | */ | |
028fec41 DR |
439 | static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags, |
440 | nodemask_t *nodes) | |
1da177e4 LT |
441 | { |
442 | struct mempolicy *policy; | |
443 | ||
3e1f0645 DR |
444 | if (mode == MPOL_DEFAULT) { |
445 | if (nodes && !nodes_empty(*nodes)) | |
37012946 | 446 | return ERR_PTR(-EINVAL); |
d3a71033 | 447 | return NULL; |
37012946 | 448 | } |
3e1f0645 DR |
449 | VM_BUG_ON(!nodes); |
450 | ||
451 | /* | |
452 | * MPOL_PREFERRED cannot be used with MPOL_F_STATIC_NODES or | |
453 | * MPOL_F_RELATIVE_NODES if the nodemask is empty (local allocation). | |
454 | * All other modes require a valid pointer to a non-empty nodemask. | |
455 | */ | |
456 | if (mode == MPOL_PREFERRED) { | |
457 | if (nodes_empty(*nodes)) { | |
458 | if (((flags & MPOL_F_STATIC_NODES) || | |
459 | (flags & MPOL_F_RELATIVE_NODES))) | |
460 | return ERR_PTR(-EINVAL); | |
7858d7bc FT |
461 | |
462 | mode = MPOL_LOCAL; | |
3e1f0645 | 463 | } |
479e2802 | 464 | } else if (mode == MPOL_LOCAL) { |
8d303e44 PK |
465 | if (!nodes_empty(*nodes) || |
466 | (flags & MPOL_F_STATIC_NODES) || | |
467 | (flags & MPOL_F_RELATIVE_NODES)) | |
479e2802 | 468 | return ERR_PTR(-EINVAL); |
3e1f0645 DR |
469 | } else if (nodes_empty(*nodes)) |
470 | return ERR_PTR(-EINVAL); | |
c36f6e6d | 471 | |
1da177e4 LT |
472 | policy = kmem_cache_alloc(policy_cache, GFP_KERNEL); |
473 | if (!policy) | |
474 | return ERR_PTR(-ENOMEM); | |
475 | atomic_set(&policy->refcnt, 1); | |
45c4745a | 476 | policy->mode = mode; |
3e1f0645 | 477 | policy->flags = flags; |
c6018b4b | 478 | policy->home_node = NUMA_NO_NODE; |
37012946 | 479 | |
1da177e4 | 480 | return policy; |
37012946 DR |
481 | } |
482 | ||
52cd3b07 | 483 | /* Slow path of a mpol destructor. */ |
c36f6e6d | 484 | void __mpol_put(struct mempolicy *pol) |
52cd3b07 | 485 | { |
c36f6e6d | 486 | if (!atomic_dec_and_test(&pol->refcnt)) |
52cd3b07 | 487 | return; |
c36f6e6d | 488 | kmem_cache_free(policy_cache, pol); |
52cd3b07 LS |
489 | } |
490 | ||
213980c0 | 491 | static void mpol_rebind_default(struct mempolicy *pol, const nodemask_t *nodes) |
37012946 DR |
492 | { |
493 | } | |
494 | ||
213980c0 | 495 | static void mpol_rebind_nodemask(struct mempolicy *pol, const nodemask_t *nodes) |
37012946 DR |
496 | { |
497 | nodemask_t tmp; | |
498 | ||
499 | if (pol->flags & MPOL_F_STATIC_NODES) | |
500 | nodes_and(tmp, pol->w.user_nodemask, *nodes); | |
501 | else if (pol->flags & MPOL_F_RELATIVE_NODES) | |
502 | mpol_relative_nodemask(&tmp, &pol->w.user_nodemask, nodes); | |
503 | else { | |
269fbe72 | 504 | nodes_remap(tmp, pol->nodes, pol->w.cpuset_mems_allowed, |
213980c0 | 505 | *nodes); |
29b190fa | 506 | pol->w.cpuset_mems_allowed = *nodes; |
37012946 | 507 | } |
f5b087b5 | 508 | |
708c1bbc MX |
509 | if (nodes_empty(tmp)) |
510 | tmp = *nodes; | |
511 | ||
269fbe72 | 512 | pol->nodes = tmp; |
37012946 DR |
513 | } |
514 | ||
515 | static void mpol_rebind_preferred(struct mempolicy *pol, | |
213980c0 | 516 | const nodemask_t *nodes) |
37012946 | 517 | { |
7858d7bc | 518 | pol->w.cpuset_mems_allowed = *nodes; |
1da177e4 LT |
519 | } |
520 | ||
708c1bbc MX |
521 | /* |
522 | * mpol_rebind_policy - Migrate a policy to a different set of nodes | |
523 | * | |
c1e8d7c6 | 524 | * Per-vma policies are protected by mmap_lock. Allocations using per-task |
213980c0 VB |
525 | * policies are protected by task->mems_allowed_seq to prevent a premature |
526 | * OOM/allocation failure due to parallel nodemask modification. | |
708c1bbc | 527 | */ |
213980c0 | 528 | static void mpol_rebind_policy(struct mempolicy *pol, const nodemask_t *newmask) |
1d0d2680 | 529 | { |
018160ad | 530 | if (!pol || pol->mode == MPOL_LOCAL) |
1d0d2680 | 531 | return; |
7858d7bc | 532 | if (!mpol_store_user_nodemask(pol) && |
1d0d2680 DR |
533 | nodes_equal(pol->w.cpuset_mems_allowed, *newmask)) |
534 | return; | |
708c1bbc | 535 | |
213980c0 | 536 | mpol_ops[pol->mode].rebind(pol, newmask); |
1d0d2680 DR |
537 | } |
538 | ||
539 | /* | |
540 | * Wrapper for mpol_rebind_policy() that just requires task | |
541 | * pointer, and updates task mempolicy. | |
58568d2a MX |
542 | * |
543 | * Called with task's alloc_lock held. | |
1d0d2680 | 544 | */ |
213980c0 | 545 | void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new) |
1d0d2680 | 546 | { |
213980c0 | 547 | mpol_rebind_policy(tsk->mempolicy, new); |
1d0d2680 DR |
548 | } |
549 | ||
550 | /* | |
551 | * Rebind each vma in mm to new nodemask. | |
552 | * | |
c1e8d7c6 | 553 | * Call holding a reference to mm. Takes mm->mmap_lock during call. |
1d0d2680 | 554 | */ |
1d0d2680 DR |
555 | void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new) |
556 | { | |
557 | struct vm_area_struct *vma; | |
66850be5 | 558 | VMA_ITERATOR(vmi, mm, 0); |
1d0d2680 | 559 | |
d8ed45c5 | 560 | mmap_write_lock(mm); |
6c21e066 JH |
561 | for_each_vma(vmi, vma) { |
562 | vma_start_write(vma); | |
213980c0 | 563 | mpol_rebind_policy(vma->vm_policy, new); |
6c21e066 | 564 | } |
d8ed45c5 | 565 | mmap_write_unlock(mm); |
1d0d2680 DR |
566 | } |
567 | ||
37012946 DR |
568 | static const struct mempolicy_operations mpol_ops[MPOL_MAX] = { |
569 | [MPOL_DEFAULT] = { | |
570 | .rebind = mpol_rebind_default, | |
571 | }, | |
572 | [MPOL_INTERLEAVE] = { | |
be897d48 | 573 | .create = mpol_new_nodemask, |
37012946 DR |
574 | .rebind = mpol_rebind_nodemask, |
575 | }, | |
576 | [MPOL_PREFERRED] = { | |
577 | .create = mpol_new_preferred, | |
578 | .rebind = mpol_rebind_preferred, | |
579 | }, | |
580 | [MPOL_BIND] = { | |
be897d48 | 581 | .create = mpol_new_nodemask, |
37012946 DR |
582 | .rebind = mpol_rebind_nodemask, |
583 | }, | |
7858d7bc FT |
584 | [MPOL_LOCAL] = { |
585 | .rebind = mpol_rebind_default, | |
586 | }, | |
b27abacc | 587 | [MPOL_PREFERRED_MANY] = { |
be897d48 | 588 | .create = mpol_new_nodemask, |
b27abacc DH |
589 | .rebind = mpol_rebind_preferred, |
590 | }, | |
fa3bea4e GP |
591 | [MPOL_WEIGHTED_INTERLEAVE] = { |
592 | .create = mpol_new_nodemask, | |
593 | .rebind = mpol_rebind_nodemask, | |
594 | }, | |
37012946 DR |
595 | }; |
596 | ||
1cb5d11a | 597 | static bool migrate_folio_add(struct folio *folio, struct list_head *foliolist, |
fc301289 | 598 | unsigned long flags); |
72e315f7 HD |
599 | static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *pol, |
600 | pgoff_t ilx, int *nid); | |
1a75a6c8 | 601 | |
1cb5d11a HD |
602 | static bool strictly_unmovable(unsigned long flags) |
603 | { | |
604 | /* | |
605 | * STRICT without MOVE flags lets do_mbind() fail immediately with -EIO | |
606 | * if any misplaced page is found. | |
607 | */ | |
608 | return (flags & (MPOL_MF_STRICT | MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) == | |
609 | MPOL_MF_STRICT; | |
610 | } | |
611 | ||
88c91dc5 HD |
612 | struct migration_mpol { /* for alloc_migration_target_by_mpol() */ |
613 | struct mempolicy *pol; | |
614 | pgoff_t ilx; | |
615 | }; | |
1a75a6c8 | 616 | |
6f4576e3 NH |
617 | struct queue_pages { |
618 | struct list_head *pagelist; | |
619 | unsigned long flags; | |
620 | nodemask_t *nmask; | |
f18da660 LX |
621 | unsigned long start; |
622 | unsigned long end; | |
623 | struct vm_area_struct *first; | |
1cb5d11a HD |
624 | struct folio *large; /* note last large folio encountered */ |
625 | long nr_failed; /* could not be isolated at this time */ | |
6f4576e3 NH |
626 | }; |
627 | ||
88aaa2a1 | 628 | /* |
d451b89d | 629 | * Check if the folio's nid is in qp->nmask. |
88aaa2a1 NH |
630 | * |
631 | * If MPOL_MF_INVERT is set in qp->flags, check if the nid is | |
632 | * in the invert of qp->nmask. | |
633 | */ | |
d451b89d | 634 | static inline bool queue_folio_required(struct folio *folio, |
88aaa2a1 NH |
635 | struct queue_pages *qp) |
636 | { | |
d451b89d | 637 | int nid = folio_nid(folio); |
88aaa2a1 NH |
638 | unsigned long flags = qp->flags; |
639 | ||
640 | return node_isset(nid, *qp->nmask) == !(flags & MPOL_MF_INVERT); | |
641 | } | |
642 | ||
1cb5d11a | 643 | static void queue_folios_pmd(pmd_t *pmd, struct mm_walk *walk) |
c8633798 | 644 | { |
de1f5055 | 645 | struct folio *folio; |
c8633798 | 646 | struct queue_pages *qp = walk->private; |
c8633798 NH |
647 | |
648 | if (unlikely(is_pmd_migration_entry(*pmd))) { | |
1cb5d11a HD |
649 | qp->nr_failed++; |
650 | return; | |
c8633798 | 651 | } |
e06d03d5 | 652 | folio = pmd_folio(*pmd); |
5beaee54 | 653 | if (is_huge_zero_folio(folio)) { |
e5947d23 | 654 | walk->action = ACTION_CONTINUE; |
1cb5d11a | 655 | return; |
c8633798 | 656 | } |
d451b89d | 657 | if (!queue_folio_required(folio, qp)) |
1cb5d11a HD |
658 | return; |
659 | if (!(qp->flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) || | |
660 | !vma_migratable(walk->vma) || | |
661 | !migrate_folio_add(folio, qp->pagelist, qp->flags)) | |
662 | qp->nr_failed++; | |
c8633798 NH |
663 | } |
664 | ||
98094945 | 665 | /* |
1cb5d11a HD |
666 | * Scan through folios, checking if they satisfy the required conditions, |
667 | * moving them from LRU to local pagelist for migration if they do (or not). | |
d8835445 | 668 | * |
1cb5d11a HD |
669 | * queue_folios_pte_range() has two possible return values: |
670 | * 0 - continue walking to scan for more, even if an existing folio on the | |
671 | * wrong node could not be isolated and queued for migration. | |
672 | * -EIO - only MPOL_MF_STRICT was specified, without MPOL_MF_MOVE or ..._ALL, | |
673 | * and an existing folio was on a node that does not follow the policy. | |
98094945 | 674 | */ |
3dae02bb | 675 | static int queue_folios_pte_range(pmd_t *pmd, unsigned long addr, |
6f4576e3 | 676 | unsigned long end, struct mm_walk *walk) |
1da177e4 | 677 | { |
4a34c584 | 678 | const fpb_t fpb_flags = FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRTY; |
6f4576e3 | 679 | struct vm_area_struct *vma = walk->vma; |
3dae02bb | 680 | struct folio *folio; |
6f4576e3 NH |
681 | struct queue_pages *qp = walk->private; |
682 | unsigned long flags = qp->flags; | |
3f088420 | 683 | pte_t *pte, *mapped_pte; |
c33c7948 | 684 | pte_t ptent; |
705e87c0 | 685 | spinlock_t *ptl; |
4a34c584 | 686 | int max_nr, nr; |
941150a3 | 687 | |
c8633798 | 688 | ptl = pmd_trans_huge_lock(pmd, vma); |
1cb5d11a HD |
689 | if (ptl) { |
690 | queue_folios_pmd(pmd, walk); | |
691 | spin_unlock(ptl); | |
692 | goto out; | |
693 | } | |
91612e0d | 694 | |
3f088420 | 695 | mapped_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); |
7780d040 HD |
696 | if (!pte) { |
697 | walk->action = ACTION_AGAIN; | |
698 | return 0; | |
699 | } | |
4a34c584 DJ |
700 | for (; addr != end; pte += nr, addr += nr * PAGE_SIZE) { |
701 | max_nr = (end - addr) >> PAGE_SHIFT; | |
702 | nr = 1; | |
c33c7948 | 703 | ptent = ptep_get(pte); |
1cb5d11a | 704 | if (pte_none(ptent)) |
1da177e4 | 705 | continue; |
1cb5d11a HD |
706 | if (!pte_present(ptent)) { |
707 | if (is_migration_entry(pte_to_swp_entry(ptent))) | |
708 | qp->nr_failed++; | |
1da177e4 | 709 | continue; |
1cb5d11a | 710 | } |
c33c7948 | 711 | folio = vm_normal_folio(vma, addr, ptent); |
3dae02bb | 712 | if (!folio || folio_is_zone_device(folio)) |
1da177e4 | 713 | continue; |
4a34c584 DJ |
714 | if (folio_test_large(folio) && max_nr != 1) |
715 | nr = folio_pte_batch(folio, addr, pte, ptent, | |
716 | max_nr, fpb_flags, | |
717 | NULL, NULL, NULL); | |
053837fc | 718 | /* |
3dae02bb VMO |
719 | * vm_normal_folio() filters out zero pages, but there might |
720 | * still be reserved folios to skip, perhaps in a VDSO. | |
053837fc | 721 | */ |
3dae02bb | 722 | if (folio_test_reserved(folio)) |
f4598c8b | 723 | continue; |
d451b89d | 724 | if (!queue_folio_required(folio, qp)) |
38e35860 | 725 | continue; |
1cb5d11a | 726 | if (folio_test_large(folio)) { |
a53190a4 | 727 | /* |
1cb5d11a HD |
728 | * A large folio can only be isolated from LRU once, |
729 | * but may be mapped by many PTEs (and Copy-On-Write may | |
730 | * intersperse PTEs of other, order 0, folios). This is | |
731 | * a common case, so don't mistake it for failure (but | |
732 | * there can be other cases of multi-mapped pages which | |
733 | * this quick check does not help to filter out - and a | |
734 | * search of the pagelist might grow to be prohibitive). | |
735 | * | |
736 | * migrate_pages(&pagelist) returns nr_failed folios, so | |
737 | * check "large" now so that queue_pages_range() returns | |
738 | * a comparable nr_failed folios. This does imply that | |
739 | * if folio could not be isolated for some racy reason | |
740 | * at its first PTE, later PTEs will not give it another | |
741 | * chance of isolation; but keeps the accounting simple. | |
a53190a4 | 742 | */ |
1cb5d11a HD |
743 | if (folio == qp->large) |
744 | continue; | |
745 | qp->large = folio; | |
746 | } | |
747 | if (!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) || | |
748 | !vma_migratable(vma) || | |
749 | !migrate_folio_add(folio, qp->pagelist, flags)) { | |
4a34c584 | 750 | qp->nr_failed += nr; |
1cb5d11a HD |
751 | if (strictly_unmovable(flags)) |
752 | break; | |
753 | } | |
6f4576e3 | 754 | } |
3f088420 | 755 | pte_unmap_unlock(mapped_pte, ptl); |
6f4576e3 | 756 | cond_resched(); |
1cb5d11a HD |
757 | out: |
758 | if (qp->nr_failed && strictly_unmovable(flags)) | |
759 | return -EIO; | |
760 | return 0; | |
91612e0d HD |
761 | } |
762 | ||
0a2c1e81 | 763 | static int queue_folios_hugetlb(pte_t *pte, unsigned long hmask, |
6f4576e3 NH |
764 | unsigned long addr, unsigned long end, |
765 | struct mm_walk *walk) | |
e2d8cf40 NH |
766 | { |
767 | #ifdef CONFIG_HUGETLB_PAGE | |
6f4576e3 | 768 | struct queue_pages *qp = walk->private; |
1cb5d11a | 769 | unsigned long flags = qp->flags; |
0a2c1e81 | 770 | struct folio *folio; |
cb900f41 | 771 | spinlock_t *ptl; |
d4c54919 | 772 | pte_t entry; |
e2d8cf40 | 773 | |
6f4576e3 | 774 | ptl = huge_pte_lock(hstate_vma(walk->vma), walk->mm, pte); |
e6c0c032 | 775 | entry = huge_ptep_get(walk->mm, addr, pte); |
1cb5d11a HD |
776 | if (!pte_present(entry)) { |
777 | if (unlikely(is_hugetlb_entry_migration(entry))) | |
778 | qp->nr_failed++; | |
d4c54919 | 779 | goto unlock; |
1cb5d11a | 780 | } |
0a2c1e81 | 781 | folio = pfn_folio(pte_pfn(entry)); |
d451b89d | 782 | if (!queue_folio_required(folio, qp)) |
e2d8cf40 | 783 | goto unlock; |
1cb5d11a HD |
784 | if (!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) || |
785 | !vma_migratable(walk->vma)) { | |
786 | qp->nr_failed++; | |
dcf17635 LX |
787 | goto unlock; |
788 | } | |
0a2c1e81 | 789 | /* |
1cb5d11a HD |
790 | * Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. |
791 | * Choosing not to migrate a shared folio is not counted as a failure. | |
0a2c1e81 | 792 | * |
003fde44 | 793 | * See folio_maybe_mapped_shared() on possible imprecision when we |
ebb34f78 | 794 | * cannot easily detect if a folio is shared. |
0a2c1e81 | 795 | */ |
1cb5d11a | 796 | if ((flags & MPOL_MF_MOVE_ALL) || |
003fde44 | 797 | (!folio_maybe_mapped_shared(folio) && !hugetlb_pmd_shared(pte))) |
4c640f12 | 798 | if (!folio_isolate_hugetlb(folio, qp->pagelist)) |
1cb5d11a | 799 | qp->nr_failed++; |
e2d8cf40 | 800 | unlock: |
cb900f41 | 801 | spin_unlock(ptl); |
1cb5d11a HD |
802 | if (qp->nr_failed && strictly_unmovable(flags)) |
803 | return -EIO; | |
e2d8cf40 | 804 | #endif |
1cb5d11a | 805 | return 0; |
1da177e4 LT |
806 | } |
807 | ||
5877231f | 808 | #ifdef CONFIG_NUMA_BALANCING |
b24f53a0 | 809 | /* |
4b10e7d5 MG |
810 | * This is used to mark a range of virtual addresses to be inaccessible. |
811 | * These are later cleared by a NUMA hinting fault. Depending on these | |
812 | * faults, pages may be migrated for better NUMA placement. | |
813 | * | |
814 | * This is assuming that NUMA faults are handled using PROT_NONE. If | |
815 | * an architecture makes a different choice, it will need further | |
816 | * changes to the core. | |
b24f53a0 | 817 | */ |
4b10e7d5 MG |
818 | unsigned long change_prot_numa(struct vm_area_struct *vma, |
819 | unsigned long addr, unsigned long end) | |
b24f53a0 | 820 | { |
4a18419f | 821 | struct mmu_gather tlb; |
a79390f5 | 822 | long nr_updated; |
b24f53a0 | 823 | |
4a18419f NA |
824 | tlb_gather_mmu(&tlb, vma->vm_mm); |
825 | ||
1ef488ed | 826 | nr_updated = change_protection(&tlb, vma, addr, end, MM_CP_PROT_NUMA); |
f77f0c75 | 827 | if (nr_updated > 0) { |
03c5a6e1 | 828 | count_vm_numa_events(NUMA_PTE_UPDATES, nr_updated); |
f77f0c75 KZ |
829 | count_memcg_events_mm(vma->vm_mm, NUMA_PTE_UPDATES, nr_updated); |
830 | } | |
b24f53a0 | 831 | |
4a18419f NA |
832 | tlb_finish_mmu(&tlb); |
833 | ||
4b10e7d5 | 834 | return nr_updated; |
b24f53a0 | 835 | } |
5877231f | 836 | #endif /* CONFIG_NUMA_BALANCING */ |
b24f53a0 | 837 | |
6f4576e3 NH |
838 | static int queue_pages_test_walk(unsigned long start, unsigned long end, |
839 | struct mm_walk *walk) | |
840 | { | |
66850be5 | 841 | struct vm_area_struct *next, *vma = walk->vma; |
6f4576e3 | 842 | struct queue_pages *qp = walk->private; |
6f4576e3 NH |
843 | unsigned long flags = qp->flags; |
844 | ||
a18b3ac2 | 845 | /* range check first */ |
ce33135c | 846 | VM_BUG_ON_VMA(!range_in_vma(vma, start, end), vma); |
f18da660 LX |
847 | |
848 | if (!qp->first) { | |
849 | qp->first = vma; | |
850 | if (!(flags & MPOL_MF_DISCONTIG_OK) && | |
851 | (qp->start < vma->vm_start)) | |
852 | /* hole at head side of range */ | |
a18b3ac2 LX |
853 | return -EFAULT; |
854 | } | |
66850be5 | 855 | next = find_vma(vma->vm_mm, vma->vm_end); |
f18da660 LX |
856 | if (!(flags & MPOL_MF_DISCONTIG_OK) && |
857 | ((vma->vm_end < qp->end) && | |
66850be5 | 858 | (!next || vma->vm_end < next->vm_start))) |
f18da660 LX |
859 | /* hole at middle or tail of range */ |
860 | return -EFAULT; | |
a18b3ac2 | 861 | |
a7f40cfe YS |
862 | /* |
863 | * Need check MPOL_MF_STRICT to return -EIO if possible | |
864 | * regardless of vma_migratable | |
865 | */ | |
866 | if (!vma_migratable(vma) && | |
867 | !(flags & MPOL_MF_STRICT)) | |
48684a65 NH |
868 | return 1; |
869 | ||
1cb5d11a HD |
870 | /* |
871 | * Check page nodes, and queue pages to move, in the current vma. | |
872 | * But if no moving, and no strict checking, the scan can be skipped. | |
873 | */ | |
874 | if (flags & (MPOL_MF_STRICT | MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) | |
6f4576e3 NH |
875 | return 0; |
876 | return 1; | |
877 | } | |
878 | ||
7b86ac33 | 879 | static const struct mm_walk_ops queue_pages_walk_ops = { |
0a2c1e81 | 880 | .hugetlb_entry = queue_folios_hugetlb, |
3dae02bb | 881 | .pmd_entry = queue_folios_pte_range, |
7b86ac33 | 882 | .test_walk = queue_pages_test_walk, |
49b06385 SB |
883 | .walk_lock = PGWALK_RDLOCK, |
884 | }; | |
885 | ||
886 | static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = { | |
887 | .hugetlb_entry = queue_folios_hugetlb, | |
888 | .pmd_entry = queue_folios_pte_range, | |
889 | .test_walk = queue_pages_test_walk, | |
890 | .walk_lock = PGWALK_WRLOCK, | |
7b86ac33 CH |
891 | }; |
892 | ||
dc9aa5b9 | 893 | /* |
98094945 NH |
894 | * Walk through page tables and collect pages to be migrated. |
895 | * | |
1cb5d11a HD |
896 | * If pages found in a given range are not on the required set of @nodes, |
897 | * and migration is allowed, they are isolated and queued to @pagelist. | |
d8835445 | 898 | * |
1cb5d11a HD |
899 | * queue_pages_range() may return: |
900 | * 0 - all pages already on the right node, or successfully queued for moving | |
901 | * (or neither strict checking nor moving requested: only range checking). | |
902 | * >0 - this number of misplaced folios could not be queued for moving | |
903 | * (a hugetlbfs page or a transparent huge page being counted as 1). | |
904 | * -EIO - a misplaced page found, when MPOL_MF_STRICT specified without MOVEs. | |
905 | * -EFAULT - a hole in the memory range, when MPOL_MF_DISCONTIG_OK unspecified. | |
dc9aa5b9 | 906 | */ |
1cb5d11a | 907 | static long |
98094945 | 908 | queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, |
6f4576e3 | 909 | nodemask_t *nodes, unsigned long flags, |
1cb5d11a | 910 | struct list_head *pagelist) |
1da177e4 | 911 | { |
f18da660 | 912 | int err; |
6f4576e3 NH |
913 | struct queue_pages qp = { |
914 | .pagelist = pagelist, | |
915 | .flags = flags, | |
916 | .nmask = nodes, | |
f18da660 LX |
917 | .start = start, |
918 | .end = end, | |
919 | .first = NULL, | |
6f4576e3 | 920 | }; |
1cb5d11a | 921 | const struct mm_walk_ops *ops = (flags & MPOL_MF_WRLOCK) ? |
49b06385 | 922 | &queue_pages_lock_vma_walk_ops : &queue_pages_walk_ops; |
6f4576e3 | 923 | |
49b06385 | 924 | err = walk_page_range(mm, start, end, ops, &qp); |
f18da660 LX |
925 | |
926 | if (!qp.first) | |
927 | /* whole range in hole */ | |
928 | err = -EFAULT; | |
929 | ||
1cb5d11a | 930 | return err ? : qp.nr_failed; |
1da177e4 LT |
931 | } |
932 | ||
869833f2 KM |
933 | /* |
934 | * Apply policy to a single VMA | |
c1e8d7c6 | 935 | * This must be called with the mmap_lock held for writing. |
869833f2 KM |
936 | */ |
937 | static int vma_replace_policy(struct vm_area_struct *vma, | |
c36f6e6d | 938 | struct mempolicy *pol) |
8d34694c | 939 | { |
869833f2 KM |
940 | int err; |
941 | struct mempolicy *old; | |
942 | struct mempolicy *new; | |
8d34694c | 943 | |
6c21e066 JH |
944 | vma_assert_write_locked(vma); |
945 | ||
869833f2 KM |
946 | new = mpol_dup(pol); |
947 | if (IS_ERR(new)) | |
948 | return PTR_ERR(new); | |
949 | ||
950 | if (vma->vm_ops && vma->vm_ops->set_policy) { | |
8d34694c | 951 | err = vma->vm_ops->set_policy(vma, new); |
869833f2 KM |
952 | if (err) |
953 | goto err_out; | |
8d34694c | 954 | } |
869833f2 KM |
955 | |
956 | old = vma->vm_policy; | |
c1e8d7c6 | 957 | vma->vm_policy = new; /* protected by mmap_lock */ |
869833f2 KM |
958 | mpol_put(old); |
959 | ||
960 | return 0; | |
961 | err_out: | |
962 | mpol_put(new); | |
8d34694c KM |
963 | return err; |
964 | } | |
965 | ||
f4e9e0e6 LH |
966 | /* Split or merge the VMA (if required) and apply the new policy */ |
967 | static int mbind_range(struct vma_iterator *vmi, struct vm_area_struct *vma, | |
968 | struct vm_area_struct **prev, unsigned long start, | |
969 | unsigned long end, struct mempolicy *new_pol) | |
1da177e4 | 970 | { |
f4e9e0e6 | 971 | unsigned long vmstart, vmend; |
9d8cebd4 | 972 | |
f4e9e0e6 LH |
973 | vmend = min(end, vma->vm_end); |
974 | if (start > vma->vm_start) { | |
975 | *prev = vma; | |
976 | vmstart = start; | |
977 | } else { | |
978 | vmstart = vma->vm_start; | |
979 | } | |
980 | ||
c36f6e6d | 981 | if (mpol_equal(vma->vm_policy, new_pol)) { |
00ca0f2e | 982 | *prev = vma; |
7329e3eb | 983 | return 0; |
00ca0f2e | 984 | } |
7329e3eb | 985 | |
94d7d923 LS |
986 | vma = vma_modify_policy(vmi, *prev, vma, vmstart, vmend, new_pol); |
987 | if (IS_ERR(vma)) | |
988 | return PTR_ERR(vma); | |
f4e9e0e6 LH |
989 | |
990 | *prev = vma; | |
991 | return vma_replace_policy(vma, new_pol); | |
1da177e4 LT |
992 | } |
993 | ||
1da177e4 | 994 | /* Set the process memory policy */ |
028fec41 DR |
995 | static long do_set_mempolicy(unsigned short mode, unsigned short flags, |
996 | nodemask_t *nodes) | |
1da177e4 | 997 | { |
58568d2a | 998 | struct mempolicy *new, *old; |
4bfc4495 | 999 | NODEMASK_SCRATCH(scratch); |
58568d2a | 1000 | int ret; |
1da177e4 | 1001 | |
4bfc4495 KH |
1002 | if (!scratch) |
1003 | return -ENOMEM; | |
f4e53d91 | 1004 | |
4bfc4495 KH |
1005 | new = mpol_new(mode, flags, nodes); |
1006 | if (IS_ERR(new)) { | |
1007 | ret = PTR_ERR(new); | |
1008 | goto out; | |
1009 | } | |
2c7c3a7d | 1010 | |
12c1dc8e | 1011 | task_lock(current); |
4bfc4495 | 1012 | ret = mpol_set_nodemask(new, nodes, scratch); |
58568d2a | 1013 | if (ret) { |
12c1dc8e | 1014 | task_unlock(current); |
58568d2a | 1015 | mpol_put(new); |
4bfc4495 | 1016 | goto out; |
58568d2a | 1017 | } |
12c1dc8e | 1018 | |
58568d2a | 1019 | old = current->mempolicy; |
1da177e4 | 1020 | current->mempolicy = new; |
fa3bea4e GP |
1021 | if (new && (new->mode == MPOL_INTERLEAVE || |
1022 | new->mode == MPOL_WEIGHTED_INTERLEAVE)) { | |
45816682 | 1023 | current->il_prev = MAX_NUMNODES-1; |
fa3bea4e GP |
1024 | current->il_weight = 0; |
1025 | } | |
58568d2a | 1026 | task_unlock(current); |
58568d2a | 1027 | mpol_put(old); |
4bfc4495 KH |
1028 | ret = 0; |
1029 | out: | |
1030 | NODEMASK_SCRATCH_FREE(scratch); | |
1031 | return ret; | |
1da177e4 LT |
1032 | } |
1033 | ||
bea904d5 LS |
1034 | /* |
1035 | * Return nodemask for policy for get_mempolicy() query | |
58568d2a MX |
1036 | * |
1037 | * Called with task's alloc_lock held | |
bea904d5 | 1038 | */ |
c36f6e6d | 1039 | static void get_policy_nodemask(struct mempolicy *pol, nodemask_t *nodes) |
1da177e4 | 1040 | { |
dfcd3c0d | 1041 | nodes_clear(*nodes); |
c36f6e6d | 1042 | if (pol == &default_policy) |
bea904d5 LS |
1043 | return; |
1044 | ||
c36f6e6d | 1045 | switch (pol->mode) { |
19770b32 | 1046 | case MPOL_BIND: |
1da177e4 | 1047 | case MPOL_INTERLEAVE: |
269fbe72 | 1048 | case MPOL_PREFERRED: |
b27abacc | 1049 | case MPOL_PREFERRED_MANY: |
fa3bea4e | 1050 | case MPOL_WEIGHTED_INTERLEAVE: |
c36f6e6d | 1051 | *nodes = pol->nodes; |
1da177e4 | 1052 | break; |
7858d7bc FT |
1053 | case MPOL_LOCAL: |
1054 | /* return empty node mask for local allocation */ | |
1055 | break; | |
1da177e4 LT |
1056 | default: |
1057 | BUG(); | |
1058 | } | |
1059 | } | |
1060 | ||
3b9aadf7 | 1061 | static int lookup_node(struct mm_struct *mm, unsigned long addr) |
1da177e4 | 1062 | { |
ba841078 | 1063 | struct page *p = NULL; |
f728b9c4 | 1064 | int ret; |
1da177e4 | 1065 | |
f728b9c4 JH |
1066 | ret = get_user_pages_fast(addr & PAGE_MASK, 1, 0, &p); |
1067 | if (ret > 0) { | |
1068 | ret = page_to_nid(p); | |
1da177e4 LT |
1069 | put_page(p); |
1070 | } | |
f728b9c4 | 1071 | return ret; |
1da177e4 LT |
1072 | } |
1073 | ||
1da177e4 | 1074 | /* Retrieve NUMA policy */ |
dbcb0f19 AB |
1075 | static long do_get_mempolicy(int *policy, nodemask_t *nmask, |
1076 | unsigned long addr, unsigned long flags) | |
1da177e4 | 1077 | { |
8bccd85f | 1078 | int err; |
1da177e4 LT |
1079 | struct mm_struct *mm = current->mm; |
1080 | struct vm_area_struct *vma = NULL; | |
3b9aadf7 | 1081 | struct mempolicy *pol = current->mempolicy, *pol_refcount = NULL; |
1da177e4 | 1082 | |
754af6f5 LS |
1083 | if (flags & |
1084 | ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED)) | |
1da177e4 | 1085 | return -EINVAL; |
754af6f5 LS |
1086 | |
1087 | if (flags & MPOL_F_MEMS_ALLOWED) { | |
1088 | if (flags & (MPOL_F_NODE|MPOL_F_ADDR)) | |
1089 | return -EINVAL; | |
1090 | *policy = 0; /* just so it's initialized */ | |
58568d2a | 1091 | task_lock(current); |
754af6f5 | 1092 | *nmask = cpuset_current_mems_allowed; |
58568d2a | 1093 | task_unlock(current); |
754af6f5 LS |
1094 | return 0; |
1095 | } | |
1096 | ||
1da177e4 | 1097 | if (flags & MPOL_F_ADDR) { |
ddc1a5cb | 1098 | pgoff_t ilx; /* ignored here */ |
bea904d5 LS |
1099 | /* |
1100 | * Do NOT fall back to task policy if the | |
1101 | * vma/shared policy at addr is NULL. We | |
1102 | * want to return MPOL_DEFAULT in this case. | |
1103 | */ | |
d8ed45c5 | 1104 | mmap_read_lock(mm); |
33e3575c | 1105 | vma = vma_lookup(mm, addr); |
1da177e4 | 1106 | if (!vma) { |
d8ed45c5 | 1107 | mmap_read_unlock(mm); |
1da177e4 LT |
1108 | return -EFAULT; |
1109 | } | |
ddc1a5cb | 1110 | pol = __get_vma_policy(vma, addr, &ilx); |
1da177e4 LT |
1111 | } else if (addr) |
1112 | return -EINVAL; | |
1113 | ||
1114 | if (!pol) | |
bea904d5 | 1115 | pol = &default_policy; /* indicates default behavior */ |
1da177e4 LT |
1116 | |
1117 | if (flags & MPOL_F_NODE) { | |
1118 | if (flags & MPOL_F_ADDR) { | |
3b9aadf7 | 1119 | /* |
f728b9c4 JH |
1120 | * Take a refcount on the mpol, because we are about to |
1121 | * drop the mmap_lock, after which only "pol" remains | |
1122 | * valid, "vma" is stale. | |
3b9aadf7 AA |
1123 | */ |
1124 | pol_refcount = pol; | |
1125 | vma = NULL; | |
1126 | mpol_get(pol); | |
f728b9c4 | 1127 | mmap_read_unlock(mm); |
3b9aadf7 | 1128 | err = lookup_node(mm, addr); |
1da177e4 LT |
1129 | if (err < 0) |
1130 | goto out; | |
8bccd85f | 1131 | *policy = err; |
1da177e4 | 1132 | } else if (pol == current->mempolicy && |
45c4745a | 1133 | pol->mode == MPOL_INTERLEAVE) { |
269fbe72 | 1134 | *policy = next_node_in(current->il_prev, pol->nodes); |
fa3bea4e GP |
1135 | } else if (pol == current->mempolicy && |
1136 | pol->mode == MPOL_WEIGHTED_INTERLEAVE) { | |
1137 | if (current->il_weight) | |
1138 | *policy = current->il_prev; | |
1139 | else | |
1140 | *policy = next_node_in(current->il_prev, | |
1141 | pol->nodes); | |
1da177e4 LT |
1142 | } else { |
1143 | err = -EINVAL; | |
1144 | goto out; | |
1145 | } | |
bea904d5 LS |
1146 | } else { |
1147 | *policy = pol == &default_policy ? MPOL_DEFAULT : | |
1148 | pol->mode; | |
d79df630 DR |
1149 | /* |
1150 | * Internal mempolicy flags must be masked off before exposing | |
1151 | * the policy to userspace. | |
1152 | */ | |
1153 | *policy |= (pol->flags & MPOL_MODE_FLAGS); | |
bea904d5 | 1154 | } |
1da177e4 | 1155 | |
1da177e4 | 1156 | err = 0; |
58568d2a | 1157 | if (nmask) { |
c6b6ef8b LS |
1158 | if (mpol_store_user_nodemask(pol)) { |
1159 | *nmask = pol->w.user_nodemask; | |
1160 | } else { | |
1161 | task_lock(current); | |
1162 | get_policy_nodemask(pol, nmask); | |
1163 | task_unlock(current); | |
1164 | } | |
58568d2a | 1165 | } |
1da177e4 LT |
1166 | |
1167 | out: | |
52cd3b07 | 1168 | mpol_cond_put(pol); |
1da177e4 | 1169 | if (vma) |
d8ed45c5 | 1170 | mmap_read_unlock(mm); |
3b9aadf7 AA |
1171 | if (pol_refcount) |
1172 | mpol_put(pol_refcount); | |
1da177e4 LT |
1173 | return err; |
1174 | } | |
1175 | ||
b20a3503 | 1176 | #ifdef CONFIG_MIGRATION |
1cb5d11a | 1177 | static bool migrate_folio_add(struct folio *folio, struct list_head *foliolist, |
fc301289 | 1178 | unsigned long flags) |
6ce3c4c0 CL |
1179 | { |
1180 | /* | |
1cb5d11a HD |
1181 | * Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. |
1182 | * Choosing not to migrate a shared folio is not counted as a failure. | |
4a64981d | 1183 | * |
003fde44 | 1184 | * See folio_maybe_mapped_shared() on possible imprecision when we |
ebb34f78 | 1185 | * cannot easily detect if a folio is shared. |
6ce3c4c0 | 1186 | */ |
003fde44 | 1187 | if ((flags & MPOL_MF_MOVE_ALL) || !folio_maybe_mapped_shared(folio)) { |
be2d5756 | 1188 | if (folio_isolate_lru(folio)) { |
4a64981d VMO |
1189 | list_add_tail(&folio->lru, foliolist); |
1190 | node_stat_mod_folio(folio, | |
1191 | NR_ISOLATED_ANON + folio_is_file_lru(folio), | |
1192 | folio_nr_pages(folio)); | |
1cb5d11a | 1193 | } else { |
a53190a4 | 1194 | /* |
4a64981d VMO |
1195 | * Non-movable folio may reach here. And, there may be |
1196 | * temporary off LRU folios or non-LRU movable folios. | |
1197 | * Treat them as unmovable folios since they can't be | |
1cb5d11a | 1198 | * isolated, so they can't be moved at the moment. |
a53190a4 | 1199 | */ |
1cb5d11a | 1200 | return false; |
62695a84 NP |
1201 | } |
1202 | } | |
1cb5d11a | 1203 | return true; |
7e2ab150 | 1204 | } |
6ce3c4c0 | 1205 | |
7e2ab150 CL |
1206 | /* |
1207 | * Migrate pages from one node to a target node. | |
1208 | * Returns error or the number of pages not migrated. | |
1209 | */ | |
1cb5d11a HD |
1210 | static long migrate_to_node(struct mm_struct *mm, int source, int dest, |
1211 | int flags) | |
7e2ab150 CL |
1212 | { |
1213 | nodemask_t nmask; | |
66850be5 | 1214 | struct vm_area_struct *vma; |
7e2ab150 | 1215 | LIST_HEAD(pagelist); |
1cb5d11a HD |
1216 | long nr_failed; |
1217 | long err = 0; | |
a0976311 JK |
1218 | struct migration_target_control mtc = { |
1219 | .nid = dest, | |
1220 | .gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, | |
e42dfe4e | 1221 | .reason = MR_SYSCALL, |
a0976311 | 1222 | }; |
7e2ab150 CL |
1223 | |
1224 | nodes_clear(nmask); | |
1225 | node_set(source, nmask); | |
6ce3c4c0 | 1226 | |
1cb5d11a | 1227 | VM_BUG_ON(!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))); |
72e315f7 HD |
1228 | |
1229 | mmap_read_lock(mm); | |
1cb5d11a | 1230 | vma = find_vma(mm, 0); |
091c1dd2 DH |
1231 | if (unlikely(!vma)) { |
1232 | mmap_read_unlock(mm); | |
1233 | return 0; | |
1234 | } | |
1cb5d11a | 1235 | |
08270807 | 1236 | /* |
1cb5d11a | 1237 | * This does not migrate the range, but isolates all pages that |
08270807 | 1238 | * need migration. Between passing in the full user address |
1cb5d11a HD |
1239 | * space range and MPOL_MF_DISCONTIG_OK, this call cannot fail, |
1240 | * but passes back the count of pages which could not be isolated. | |
08270807 | 1241 | */ |
1cb5d11a HD |
1242 | nr_failed = queue_pages_range(mm, vma->vm_start, mm->task_size, &nmask, |
1243 | flags | MPOL_MF_DISCONTIG_OK, &pagelist); | |
72e315f7 | 1244 | mmap_read_unlock(mm); |
7e2ab150 | 1245 | |
cf608ac1 | 1246 | if (!list_empty(&pagelist)) { |
a0976311 | 1247 | err = migrate_pages(&pagelist, alloc_migration_target, NULL, |
1cb5d11a | 1248 | (unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL); |
cf608ac1 | 1249 | if (err) |
e2d8cf40 | 1250 | putback_movable_pages(&pagelist); |
cf608ac1 | 1251 | } |
95a402c3 | 1252 | |
1cb5d11a HD |
1253 | if (err >= 0) |
1254 | err += nr_failed; | |
7e2ab150 | 1255 | return err; |
6ce3c4c0 CL |
1256 | } |
1257 | ||
39743889 | 1258 | /* |
7e2ab150 CL |
1259 | * Move pages between the two nodesets so as to preserve the physical |
1260 | * layout as much as possible. | |
39743889 CL |
1261 | * |
1262 | * Returns the number of page that could not be moved. | |
1263 | */ | |
0ce72d4f AM |
1264 | int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from, |
1265 | const nodemask_t *to, int flags) | |
39743889 | 1266 | { |
1cb5d11a HD |
1267 | long nr_failed = 0; |
1268 | long err = 0; | |
7e2ab150 | 1269 | nodemask_t tmp; |
39743889 | 1270 | |
361a2a22 | 1271 | lru_cache_disable(); |
0aedadf9 | 1272 | |
da0aa138 KM |
1273 | /* |
1274 | * Find a 'source' bit set in 'tmp' whose corresponding 'dest' | |
1275 | * bit in 'to' is not also set in 'tmp'. Clear the found 'source' | |
1276 | * bit in 'tmp', and return that <source, dest> pair for migration. | |
1277 | * The pair of nodemasks 'to' and 'from' define the map. | |
1278 | * | |
1279 | * If no pair of bits is found that way, fallback to picking some | |
1280 | * pair of 'source' and 'dest' bits that are not the same. If the | |
1281 | * 'source' and 'dest' bits are the same, this represents a node | |
1282 | * that will be migrating to itself, so no pages need move. | |
1283 | * | |
1284 | * If no bits are left in 'tmp', or if all remaining bits left | |
1285 | * in 'tmp' correspond to the same bit in 'to', return false | |
1286 | * (nothing left to migrate). | |
1287 | * | |
1288 | * This lets us pick a pair of nodes to migrate between, such that | |
1289 | * if possible the dest node is not already occupied by some other | |
1290 | * source node, minimizing the risk of overloading the memory on a | |
1291 | * node that would happen if we migrated incoming memory to a node | |
1292 | * before migrating outgoing memory source that same node. | |
1293 | * | |
1294 | * A single scan of tmp is sufficient. As we go, we remember the | |
1295 | * most recent <s, d> pair that moved (s != d). If we find a pair | |
1296 | * that not only moved, but what's better, moved to an empty slot | |
1297 | * (d is not set in tmp), then we break out then, with that pair. | |
ae0e47f0 | 1298 | * Otherwise when we finish scanning from_tmp, we at least have the |
da0aa138 KM |
1299 | * most recent <s, d> pair that moved. If we get all the way through |
1300 | * the scan of tmp without finding any node that moved, much less | |
1301 | * moved to an empty node, then there is nothing left worth migrating. | |
1302 | */ | |
d4984711 | 1303 | |
0ce72d4f | 1304 | tmp = *from; |
7e2ab150 | 1305 | while (!nodes_empty(tmp)) { |
68d68ff6 | 1306 | int s, d; |
b76ac7e7 | 1307 | int source = NUMA_NO_NODE; |
7e2ab150 CL |
1308 | int dest = 0; |
1309 | ||
1310 | for_each_node_mask(s, tmp) { | |
4a5b18cc LW |
1311 | |
1312 | /* | |
1313 | * do_migrate_pages() tries to maintain the relative | |
1314 | * node relationship of the pages established between | |
1315 | * threads and memory areas. | |
1316 | * | |
1317 | * However if the number of source nodes is not equal to | |
1318 | * the number of destination nodes we can not preserve | |
1319 | * this node relative relationship. In that case, skip | |
1320 | * copying memory from a node that is in the destination | |
1321 | * mask. | |
1322 | * | |
1323 | * Example: [2,3,4] -> [3,4,5] moves everything. | |
1324 | * [0-7] - > [3,4,5] moves only 0,1,2,6,7. | |
1325 | */ | |
1326 | ||
0ce72d4f AM |
1327 | if ((nodes_weight(*from) != nodes_weight(*to)) && |
1328 | (node_isset(s, *to))) | |
4a5b18cc LW |
1329 | continue; |
1330 | ||
0ce72d4f | 1331 | d = node_remap(s, *from, *to); |
7e2ab150 CL |
1332 | if (s == d) |
1333 | continue; | |
1334 | ||
1335 | source = s; /* Node moved. Memorize */ | |
1336 | dest = d; | |
1337 | ||
1338 | /* dest not in remaining from nodes? */ | |
1339 | if (!node_isset(dest, tmp)) | |
1340 | break; | |
1341 | } | |
b76ac7e7 | 1342 | if (source == NUMA_NO_NODE) |
7e2ab150 CL |
1343 | break; |
1344 | ||
1345 | node_clear(source, tmp); | |
1346 | err = migrate_to_node(mm, source, dest, flags); | |
1347 | if (err > 0) | |
1cb5d11a | 1348 | nr_failed += err; |
7e2ab150 CL |
1349 | if (err < 0) |
1350 | break; | |
39743889 | 1351 | } |
d479960e | 1352 | |
361a2a22 | 1353 | lru_cache_enable(); |
7e2ab150 CL |
1354 | if (err < 0) |
1355 | return err; | |
1cb5d11a | 1356 | return (nr_failed < INT_MAX) ? nr_failed : INT_MAX; |
b20a3503 CL |
1357 | } |
1358 | ||
3ad33b24 | 1359 | /* |
72e315f7 | 1360 | * Allocate a new folio for page migration, according to NUMA mempolicy. |
3ad33b24 | 1361 | */ |
72e315f7 HD |
1362 | static struct folio *alloc_migration_target_by_mpol(struct folio *src, |
1363 | unsigned long private) | |
95a402c3 | 1364 | { |
88c91dc5 HD |
1365 | struct migration_mpol *mmpol = (struct migration_mpol *)private; |
1366 | struct mempolicy *pol = mmpol->pol; | |
1367 | pgoff_t ilx = mmpol->ilx; | |
72e315f7 HD |
1368 | unsigned int order; |
1369 | int nid = numa_node_id(); | |
1370 | gfp_t gfp; | |
95a402c3 | 1371 | |
72e315f7 HD |
1372 | order = folio_order(src); |
1373 | ilx += src->index >> order; | |
11c731e8 | 1374 | |
d0ce0e47 | 1375 | if (folio_test_hugetlb(src)) { |
72e315f7 HD |
1376 | nodemask_t *nodemask; |
1377 | struct hstate *h; | |
1378 | ||
1379 | h = folio_hstate(src); | |
1380 | gfp = htlb_alloc_mask(h); | |
1381 | nodemask = policy_nodemask(gfp, pol, ilx, &nid); | |
42d0c3fb BW |
1382 | return alloc_hugetlb_folio_nodemask(h, nid, nodemask, gfp, |
1383 | htlb_allow_alloc_fallback(MR_MEMPOLICY_MBIND)); | |
d0ce0e47 | 1384 | } |
ec4858e0 MWO |
1385 | |
1386 | if (folio_test_large(src)) | |
1387 | gfp = GFP_TRANSHUGE; | |
72e315f7 HD |
1388 | else |
1389 | gfp = GFP_HIGHUSER_MOVABLE | __GFP_RETRY_MAYFAIL | __GFP_COMP; | |
ec4858e0 | 1390 | |
1d9cb785 | 1391 | return folio_alloc_mpol(gfp, order, pol, ilx, nid); |
95a402c3 | 1392 | } |
b20a3503 CL |
1393 | #else |
1394 | ||
1cb5d11a | 1395 | static bool migrate_folio_add(struct folio *folio, struct list_head *foliolist, |
b20a3503 CL |
1396 | unsigned long flags) |
1397 | { | |
1cb5d11a | 1398 | return false; |
39743889 CL |
1399 | } |
1400 | ||
0ce72d4f AM |
1401 | int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from, |
1402 | const nodemask_t *to, int flags) | |
b20a3503 CL |
1403 | { |
1404 | return -ENOSYS; | |
1405 | } | |
95a402c3 | 1406 | |
72e315f7 HD |
1407 | static struct folio *alloc_migration_target_by_mpol(struct folio *src, |
1408 | unsigned long private) | |
95a402c3 CL |
1409 | { |
1410 | return NULL; | |
1411 | } | |
b20a3503 CL |
1412 | #endif |
1413 | ||
dbcb0f19 | 1414 | static long do_mbind(unsigned long start, unsigned long len, |
028fec41 DR |
1415 | unsigned short mode, unsigned short mode_flags, |
1416 | nodemask_t *nmask, unsigned long flags) | |
6ce3c4c0 | 1417 | { |
6ce3c4c0 | 1418 | struct mm_struct *mm = current->mm; |
f4e9e0e6 LH |
1419 | struct vm_area_struct *vma, *prev; |
1420 | struct vma_iterator vmi; | |
88c91dc5 | 1421 | struct migration_mpol mmpol; |
6ce3c4c0 CL |
1422 | struct mempolicy *new; |
1423 | unsigned long end; | |
1cb5d11a HD |
1424 | long err; |
1425 | long nr_failed; | |
6ce3c4c0 CL |
1426 | LIST_HEAD(pagelist); |
1427 | ||
b24f53a0 | 1428 | if (flags & ~(unsigned long)MPOL_MF_VALID) |
6ce3c4c0 | 1429 | return -EINVAL; |
74c00241 | 1430 | if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE)) |
6ce3c4c0 CL |
1431 | return -EPERM; |
1432 | ||
1433 | if (start & ~PAGE_MASK) | |
1434 | return -EINVAL; | |
1435 | ||
1436 | if (mode == MPOL_DEFAULT) | |
1437 | flags &= ~MPOL_MF_STRICT; | |
1438 | ||
aaa31e05 | 1439 | len = PAGE_ALIGN(len); |
6ce3c4c0 CL |
1440 | end = start + len; |
1441 | ||
1442 | if (end < start) | |
1443 | return -EINVAL; | |
1444 | if (end == start) | |
1445 | return 0; | |
1446 | ||
028fec41 | 1447 | new = mpol_new(mode, mode_flags, nmask); |
6ce3c4c0 CL |
1448 | if (IS_ERR(new)) |
1449 | return PTR_ERR(new); | |
1450 | ||
1451 | /* | |
1452 | * If we are using the default policy then operation | |
1453 | * on discontinuous address spaces is okay after all | |
1454 | */ | |
1455 | if (!new) | |
1456 | flags |= MPOL_MF_DISCONTIG_OK; | |
1457 | ||
1cb5d11a | 1458 | if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) |
361a2a22 | 1459 | lru_cache_disable(); |
4bfc4495 KH |
1460 | { |
1461 | NODEMASK_SCRATCH(scratch); | |
1462 | if (scratch) { | |
d8ed45c5 | 1463 | mmap_write_lock(mm); |
4bfc4495 | 1464 | err = mpol_set_nodemask(new, nmask, scratch); |
4bfc4495 | 1465 | if (err) |
d8ed45c5 | 1466 | mmap_write_unlock(mm); |
4bfc4495 KH |
1467 | } else |
1468 | err = -ENOMEM; | |
1469 | NODEMASK_SCRATCH_FREE(scratch); | |
1470 | } | |
b05ca738 KM |
1471 | if (err) |
1472 | goto mpol_out; | |
1473 | ||
6c21e066 | 1474 | /* |
1cb5d11a HD |
1475 | * Lock the VMAs before scanning for pages to migrate, |
1476 | * to ensure we don't miss a concurrently inserted page. | |
6c21e066 | 1477 | */ |
1cb5d11a HD |
1478 | nr_failed = queue_pages_range(mm, start, end, nmask, |
1479 | flags | MPOL_MF_INVERT | MPOL_MF_WRLOCK, &pagelist); | |
d8835445 | 1480 | |
1cb5d11a HD |
1481 | if (nr_failed < 0) { |
1482 | err = nr_failed; | |
72e315f7 | 1483 | nr_failed = 0; |
1cb5d11a HD |
1484 | } else { |
1485 | vma_iter_init(&vmi, mm, start); | |
1486 | prev = vma_prev(&vmi); | |
1487 | for_each_vma_range(vmi, vma, end) { | |
1488 | err = mbind_range(&vmi, vma, &prev, start, end, new); | |
1489 | if (err) | |
1490 | break; | |
1491 | } | |
f4e9e0e6 | 1492 | } |
7e2ab150 | 1493 | |
72e315f7 HD |
1494 | if (!err && !list_empty(&pagelist)) { |
1495 | /* Convert MPOL_DEFAULT's NULL to task or default policy */ | |
1496 | if (!new) { | |
1497 | new = get_task_policy(current); | |
1498 | mpol_get(new); | |
cf608ac1 | 1499 | } |
88c91dc5 HD |
1500 | mmpol.pol = new; |
1501 | mmpol.ilx = 0; | |
6ce3c4c0 | 1502 | |
88c91dc5 HD |
1503 | /* |
1504 | * In the interleaved case, attempt to allocate on exactly the | |
1505 | * targeted nodes, for the first VMA to be migrated; for later | |
1506 | * VMAs, the nodes will still be interleaved from the targeted | |
1507 | * nodemask, but one by one may be selected differently. | |
1508 | */ | |
fa3bea4e GP |
1509 | if (new->mode == MPOL_INTERLEAVE || |
1510 | new->mode == MPOL_WEIGHTED_INTERLEAVE) { | |
f1cce6f7 | 1511 | struct folio *folio; |
88c91dc5 HD |
1512 | unsigned int order; |
1513 | unsigned long addr = -EFAULT; | |
1514 | ||
f1cce6f7 MWO |
1515 | list_for_each_entry(folio, &pagelist, lru) { |
1516 | if (!folio_test_ksm(folio)) | |
88c91dc5 HD |
1517 | break; |
1518 | } | |
f1cce6f7 | 1519 | if (!list_entry_is_head(folio, &pagelist, lru)) { |
88c91dc5 HD |
1520 | vma_iter_init(&vmi, mm, start); |
1521 | for_each_vma_range(vmi, vma, end) { | |
713da0b3 | 1522 | addr = page_address_in_vma(folio, |
f1cce6f7 | 1523 | folio_page(folio, 0), vma); |
88c91dc5 HD |
1524 | if (addr != -EFAULT) |
1525 | break; | |
1526 | } | |
1527 | } | |
1528 | if (addr != -EFAULT) { | |
f1cce6f7 | 1529 | order = folio_order(folio); |
88c91dc5 HD |
1530 | /* We already know the pol, but not the ilx */ |
1531 | mpol_cond_put(get_vma_policy(vma, addr, order, | |
1532 | &mmpol.ilx)); | |
1533 | /* Set base from which to increment by index */ | |
f1cce6f7 | 1534 | mmpol.ilx -= folio->index >> order; |
88c91dc5 HD |
1535 | } |
1536 | } | |
a85dfc30 YS |
1537 | } |
1538 | ||
d8ed45c5 | 1539 | mmap_write_unlock(mm); |
88c91dc5 HD |
1540 | |
1541 | if (!err && !list_empty(&pagelist)) { | |
72e315f7 HD |
1542 | nr_failed |= migrate_pages(&pagelist, |
1543 | alloc_migration_target_by_mpol, NULL, | |
88c91dc5 | 1544 | (unsigned long)&mmpol, MIGRATE_SYNC, |
72e315f7 | 1545 | MR_MEMPOLICY_MBIND, NULL); |
a85dfc30 YS |
1546 | } |
1547 | ||
72e315f7 HD |
1548 | if (nr_failed && (flags & MPOL_MF_STRICT)) |
1549 | err = -EIO; | |
1cb5d11a HD |
1550 | if (!list_empty(&pagelist)) |
1551 | putback_movable_pages(&pagelist); | |
d8835445 | 1552 | mpol_out: |
f0be3d32 | 1553 | mpol_put(new); |
d479960e | 1554 | if (flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL)) |
361a2a22 | 1555 | lru_cache_enable(); |
6ce3c4c0 CL |
1556 | return err; |
1557 | } | |
1558 | ||
8bccd85f CL |
1559 | /* |
1560 | * User space interface with variable sized bitmaps for nodelists. | |
1561 | */ | |
e130242d AB |
1562 | static int get_bitmap(unsigned long *mask, const unsigned long __user *nmask, |
1563 | unsigned long maxnode) | |
1564 | { | |
1565 | unsigned long nlongs = BITS_TO_LONGS(maxnode); | |
1566 | int ret; | |
1567 | ||
1568 | if (in_compat_syscall()) | |
1569 | ret = compat_get_bitmap(mask, | |
1570 | (const compat_ulong_t __user *)nmask, | |
1571 | maxnode); | |
1572 | else | |
1573 | ret = copy_from_user(mask, nmask, | |
1574 | nlongs * sizeof(unsigned long)); | |
1575 | ||
1576 | if (ret) | |
1577 | return -EFAULT; | |
1578 | ||
1579 | if (maxnode % BITS_PER_LONG) | |
1580 | mask[nlongs - 1] &= (1UL << (maxnode % BITS_PER_LONG)) - 1; | |
1581 | ||
1582 | return 0; | |
1583 | } | |
8bccd85f CL |
1584 | |
1585 | /* Copy a node mask from user space. */ | |
39743889 | 1586 | static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask, |
8bccd85f CL |
1587 | unsigned long maxnode) |
1588 | { | |
8bccd85f CL |
1589 | --maxnode; |
1590 | nodes_clear(*nodes); | |
1591 | if (maxnode == 0 || !nmask) | |
1592 | return 0; | |
a9c930ba | 1593 | if (maxnode > PAGE_SIZE*BITS_PER_BYTE) |
636f13c1 | 1594 | return -EINVAL; |
8bccd85f | 1595 | |
56521e7a YX |
1596 | /* |
1597 | * When the user specified more nodes than supported just check | |
e130242d AB |
1598 | * if the non supported part is all zero, one word at a time, |
1599 | * starting at the end. | |
56521e7a | 1600 | */ |
e130242d AB |
1601 | while (maxnode > MAX_NUMNODES) { |
1602 | unsigned long bits = min_t(unsigned long, maxnode, BITS_PER_LONG); | |
1603 | unsigned long t; | |
8bccd85f | 1604 | |
000eca5d | 1605 | if (get_bitmap(&t, &nmask[(maxnode - 1) / BITS_PER_LONG], bits)) |
56521e7a | 1606 | return -EFAULT; |
e130242d AB |
1607 | |
1608 | if (maxnode - bits >= MAX_NUMNODES) { | |
1609 | maxnode -= bits; | |
1610 | } else { | |
1611 | maxnode = MAX_NUMNODES; | |
1612 | t &= ~((1UL << (MAX_NUMNODES % BITS_PER_LONG)) - 1); | |
1613 | } | |
1614 | if (t) | |
56521e7a YX |
1615 | return -EINVAL; |
1616 | } | |
1617 | ||
e130242d | 1618 | return get_bitmap(nodes_addr(*nodes), nmask, maxnode); |
8bccd85f CL |
1619 | } |
1620 | ||
1621 | /* Copy a kernel node mask to user space */ | |
1622 | static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode, | |
1623 | nodemask_t *nodes) | |
1624 | { | |
1625 | unsigned long copy = ALIGN(maxnode-1, 64) / 8; | |
050c17f2 | 1626 | unsigned int nbytes = BITS_TO_LONGS(nr_node_ids) * sizeof(long); |
e130242d AB |
1627 | bool compat = in_compat_syscall(); |
1628 | ||
1629 | if (compat) | |
1630 | nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t); | |
8bccd85f CL |
1631 | |
1632 | if (copy > nbytes) { | |
1633 | if (copy > PAGE_SIZE) | |
1634 | return -EINVAL; | |
1635 | if (clear_user((char __user *)mask + nbytes, copy - nbytes)) | |
1636 | return -EFAULT; | |
1637 | copy = nbytes; | |
e130242d | 1638 | maxnode = nr_node_ids; |
8bccd85f | 1639 | } |
e130242d AB |
1640 | |
1641 | if (compat) | |
1642 | return compat_put_bitmap((compat_ulong_t __user *)mask, | |
1643 | nodes_addr(*nodes), maxnode); | |
1644 | ||
8bccd85f CL |
1645 | return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0; |
1646 | } | |
1647 | ||
95837924 FT |
1648 | /* Basic parameter sanity check used by both mbind() and set_mempolicy() */ |
1649 | static inline int sanitize_mpol_flags(int *mode, unsigned short *flags) | |
1650 | { | |
1651 | *flags = *mode & MPOL_MODE_FLAGS; | |
1652 | *mode &= ~MPOL_MODE_FLAGS; | |
b27abacc | 1653 | |
a38a59fd | 1654 | if ((unsigned int)(*mode) >= MPOL_MAX) |
95837924 FT |
1655 | return -EINVAL; |
1656 | if ((*flags & MPOL_F_STATIC_NODES) && (*flags & MPOL_F_RELATIVE_NODES)) | |
1657 | return -EINVAL; | |
6d2aec9e | 1658 | if (*flags & MPOL_F_NUMA_BALANCING) { |
133d04b1 DT |
1659 | if (*mode == MPOL_BIND || *mode == MPOL_PREFERRED_MANY) |
1660 | *flags |= (MPOL_F_MOF | MPOL_F_MORON); | |
1661 | else | |
6d2aec9e | 1662 | return -EINVAL; |
6d2aec9e | 1663 | } |
95837924 FT |
1664 | return 0; |
1665 | } | |
1666 | ||
e7dc9ad6 DB |
1667 | static long kernel_mbind(unsigned long start, unsigned long len, |
1668 | unsigned long mode, const unsigned long __user *nmask, | |
1669 | unsigned long maxnode, unsigned int flags) | |
8bccd85f | 1670 | { |
95837924 | 1671 | unsigned short mode_flags; |
8bccd85f | 1672 | nodemask_t nodes; |
95837924 | 1673 | int lmode = mode; |
8bccd85f CL |
1674 | int err; |
1675 | ||
057d3389 | 1676 | start = untagged_addr(start); |
95837924 FT |
1677 | err = sanitize_mpol_flags(&lmode, &mode_flags); |
1678 | if (err) | |
1679 | return err; | |
1680 | ||
8bccd85f CL |
1681 | err = get_nodes(&nodes, nmask, maxnode); |
1682 | if (err) | |
1683 | return err; | |
95837924 FT |
1684 | |
1685 | return do_mbind(start, len, lmode, mode_flags, &nodes, flags); | |
8bccd85f CL |
1686 | } |
1687 | ||
c6018b4b AK |
1688 | SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, len, |
1689 | unsigned long, home_node, unsigned long, flags) | |
1690 | { | |
1691 | struct mm_struct *mm = current->mm; | |
f4e9e0e6 | 1692 | struct vm_area_struct *vma, *prev; |
e976936c | 1693 | struct mempolicy *new, *old; |
c6018b4b AK |
1694 | unsigned long end; |
1695 | int err = -ENOENT; | |
66850be5 | 1696 | VMA_ITERATOR(vmi, mm, start); |
c6018b4b AK |
1697 | |
1698 | start = untagged_addr(start); | |
1699 | if (start & ~PAGE_MASK) | |
1700 | return -EINVAL; | |
1701 | /* | |
1702 | * flags is used for future extension if any. | |
1703 | */ | |
1704 | if (flags != 0) | |
1705 | return -EINVAL; | |
1706 | ||
1707 | /* | |
1708 | * Check home_node is online to avoid accessing uninitialized | |
1709 | * NODE_DATA. | |
1710 | */ | |
1711 | if (home_node >= MAX_NUMNODES || !node_online(home_node)) | |
1712 | return -EINVAL; | |
1713 | ||
aaa31e05 | 1714 | len = PAGE_ALIGN(len); |
c6018b4b AK |
1715 | end = start + len; |
1716 | ||
1717 | if (end < start) | |
1718 | return -EINVAL; | |
1719 | if (end == start) | |
1720 | return 0; | |
1721 | mmap_write_lock(mm); | |
f4e9e0e6 | 1722 | prev = vma_prev(&vmi); |
66850be5 | 1723 | for_each_vma_range(vmi, vma, end) { |
c6018b4b AK |
1724 | /* |
1725 | * If any vma in the range got policy other than MPOL_BIND | |
1726 | * or MPOL_PREFERRED_MANY we return error. We don't reset | |
1727 | * the home node for vmas we already updated before. | |
1728 | */ | |
e976936c | 1729 | old = vma_policy(vma); |
51f62537 LH |
1730 | if (!old) { |
1731 | prev = vma; | |
e976936c | 1732 | continue; |
51f62537 | 1733 | } |
e976936c | 1734 | if (old->mode != MPOL_BIND && old->mode != MPOL_PREFERRED_MANY) { |
c6018b4b AK |
1735 | err = -EOPNOTSUPP; |
1736 | break; | |
1737 | } | |
e976936c MH |
1738 | new = mpol_dup(old); |
1739 | if (IS_ERR(new)) { | |
1740 | err = PTR_ERR(new); | |
1741 | break; | |
1742 | } | |
c6018b4b | 1743 | |
6c21e066 | 1744 | vma_start_write(vma); |
c6018b4b | 1745 | new->home_node = home_node; |
f4e9e0e6 | 1746 | err = mbind_range(&vmi, vma, &prev, start, end, new); |
c6018b4b AK |
1747 | mpol_put(new); |
1748 | if (err) | |
1749 | break; | |
1750 | } | |
1751 | mmap_write_unlock(mm); | |
1752 | return err; | |
1753 | } | |
1754 | ||
e7dc9ad6 DB |
1755 | SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len, |
1756 | unsigned long, mode, const unsigned long __user *, nmask, | |
1757 | unsigned long, maxnode, unsigned int, flags) | |
1758 | { | |
1759 | return kernel_mbind(start, len, mode, nmask, maxnode, flags); | |
1760 | } | |
1761 | ||
8bccd85f | 1762 | /* Set the process memory policy */ |
af03c4ac DB |
1763 | static long kernel_set_mempolicy(int mode, const unsigned long __user *nmask, |
1764 | unsigned long maxnode) | |
8bccd85f | 1765 | { |
95837924 | 1766 | unsigned short mode_flags; |
8bccd85f | 1767 | nodemask_t nodes; |
95837924 FT |
1768 | int lmode = mode; |
1769 | int err; | |
1770 | ||
1771 | err = sanitize_mpol_flags(&lmode, &mode_flags); | |
1772 | if (err) | |
1773 | return err; | |
8bccd85f | 1774 | |
8bccd85f CL |
1775 | err = get_nodes(&nodes, nmask, maxnode); |
1776 | if (err) | |
1777 | return err; | |
95837924 FT |
1778 | |
1779 | return do_set_mempolicy(lmode, mode_flags, &nodes); | |
8bccd85f CL |
1780 | } |
1781 | ||
af03c4ac DB |
1782 | SYSCALL_DEFINE3(set_mempolicy, int, mode, const unsigned long __user *, nmask, |
1783 | unsigned long, maxnode) | |
1784 | { | |
1785 | return kernel_set_mempolicy(mode, nmask, maxnode); | |
1786 | } | |
1787 | ||
b6e9b0ba DB |
1788 | static int kernel_migrate_pages(pid_t pid, unsigned long maxnode, |
1789 | const unsigned long __user *old_nodes, | |
1790 | const unsigned long __user *new_nodes) | |
39743889 | 1791 | { |
596d7cfa | 1792 | struct mm_struct *mm = NULL; |
39743889 | 1793 | struct task_struct *task; |
39743889 CL |
1794 | nodemask_t task_nodes; |
1795 | int err; | |
596d7cfa KM |
1796 | nodemask_t *old; |
1797 | nodemask_t *new; | |
1798 | NODEMASK_SCRATCH(scratch); | |
1799 | ||
1800 | if (!scratch) | |
1801 | return -ENOMEM; | |
39743889 | 1802 | |
596d7cfa KM |
1803 | old = &scratch->mask1; |
1804 | new = &scratch->mask2; | |
1805 | ||
1806 | err = get_nodes(old, old_nodes, maxnode); | |
39743889 | 1807 | if (err) |
596d7cfa | 1808 | goto out; |
39743889 | 1809 | |
596d7cfa | 1810 | err = get_nodes(new, new_nodes, maxnode); |
39743889 | 1811 | if (err) |
596d7cfa | 1812 | goto out; |
39743889 CL |
1813 | |
1814 | /* Find the mm_struct */ | |
55cfaa3c | 1815 | rcu_read_lock(); |
228ebcbe | 1816 | task = pid ? find_task_by_vpid(pid) : current; |
39743889 | 1817 | if (!task) { |
55cfaa3c | 1818 | rcu_read_unlock(); |
596d7cfa KM |
1819 | err = -ESRCH; |
1820 | goto out; | |
39743889 | 1821 | } |
3268c63e | 1822 | get_task_struct(task); |
39743889 | 1823 | |
596d7cfa | 1824 | err = -EINVAL; |
39743889 CL |
1825 | |
1826 | /* | |
31367466 OE |
1827 | * Check if this process has the right to modify the specified process. |
1828 | * Use the regular "ptrace_may_access()" checks. | |
39743889 | 1829 | */ |
31367466 | 1830 | if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) { |
c69e8d9c | 1831 | rcu_read_unlock(); |
39743889 | 1832 | err = -EPERM; |
3268c63e | 1833 | goto out_put; |
39743889 | 1834 | } |
c69e8d9c | 1835 | rcu_read_unlock(); |
39743889 CL |
1836 | |
1837 | task_nodes = cpuset_mems_allowed(task); | |
1838 | /* Is the user allowed to access the target nodes? */ | |
596d7cfa | 1839 | if (!nodes_subset(*new, task_nodes) && !capable(CAP_SYS_NICE)) { |
39743889 | 1840 | err = -EPERM; |
3268c63e | 1841 | goto out_put; |
39743889 CL |
1842 | } |
1843 | ||
0486a38b YX |
1844 | task_nodes = cpuset_mems_allowed(current); |
1845 | nodes_and(*new, *new, task_nodes); | |
1846 | if (nodes_empty(*new)) | |
1847 | goto out_put; | |
1848 | ||
86c3a764 DQ |
1849 | err = security_task_movememory(task); |
1850 | if (err) | |
3268c63e | 1851 | goto out_put; |
86c3a764 | 1852 | |
3268c63e CL |
1853 | mm = get_task_mm(task); |
1854 | put_task_struct(task); | |
f2a9ef88 SL |
1855 | |
1856 | if (!mm) { | |
3268c63e | 1857 | err = -EINVAL; |
f2a9ef88 SL |
1858 | goto out; |
1859 | } | |
1860 | ||
1861 | err = do_migrate_pages(mm, old, new, | |
1862 | capable(CAP_SYS_NICE) ? MPOL_MF_MOVE_ALL : MPOL_MF_MOVE); | |
3268c63e CL |
1863 | |
1864 | mmput(mm); | |
1865 | out: | |
596d7cfa KM |
1866 | NODEMASK_SCRATCH_FREE(scratch); |
1867 | ||
39743889 | 1868 | return err; |
3268c63e CL |
1869 | |
1870 | out_put: | |
1871 | put_task_struct(task); | |
1872 | goto out; | |
39743889 CL |
1873 | } |
1874 | ||
b6e9b0ba DB |
1875 | SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode, |
1876 | const unsigned long __user *, old_nodes, | |
1877 | const unsigned long __user *, new_nodes) | |
1878 | { | |
1879 | return kernel_migrate_pages(pid, maxnode, old_nodes, new_nodes); | |
1880 | } | |
1881 | ||
8bccd85f | 1882 | /* Retrieve NUMA policy */ |
af03c4ac DB |
1883 | static int kernel_get_mempolicy(int __user *policy, |
1884 | unsigned long __user *nmask, | |
1885 | unsigned long maxnode, | |
1886 | unsigned long addr, | |
1887 | unsigned long flags) | |
8bccd85f | 1888 | { |
dbcb0f19 | 1889 | int err; |
3f649ab7 | 1890 | int pval; |
8bccd85f CL |
1891 | nodemask_t nodes; |
1892 | ||
050c17f2 | 1893 | if (nmask != NULL && maxnode < nr_node_ids) |
8bccd85f CL |
1894 | return -EINVAL; |
1895 | ||
4605f057 WH |
1896 | addr = untagged_addr(addr); |
1897 | ||
8bccd85f CL |
1898 | err = do_get_mempolicy(&pval, &nodes, addr, flags); |
1899 | ||
1900 | if (err) | |
1901 | return err; | |
1902 | ||
1903 | if (policy && put_user(pval, policy)) | |
1904 | return -EFAULT; | |
1905 | ||
1906 | if (nmask) | |
1907 | err = copy_nodes_to_user(nmask, maxnode, &nodes); | |
1908 | ||
1909 | return err; | |
1910 | } | |
1911 | ||
af03c4ac DB |
1912 | SYSCALL_DEFINE5(get_mempolicy, int __user *, policy, |
1913 | unsigned long __user *, nmask, unsigned long, maxnode, | |
1914 | unsigned long, addr, unsigned long, flags) | |
1915 | { | |
1916 | return kernel_get_mempolicy(policy, nmask, maxnode, addr, flags); | |
1917 | } | |
1918 | ||
20ca87f2 LX |
1919 | bool vma_migratable(struct vm_area_struct *vma) |
1920 | { | |
1921 | if (vma->vm_flags & (VM_IO | VM_PFNMAP)) | |
1922 | return false; | |
1923 | ||
1924 | /* | |
1925 | * DAX device mappings require predictable access latency, so avoid | |
1926 | * incurring periodic faults. | |
1927 | */ | |
1928 | if (vma_is_dax(vma)) | |
1929 | return false; | |
1930 | ||
1931 | if (is_vm_hugetlb_page(vma) && | |
1932 | !hugepage_migration_supported(hstate_vma(vma))) | |
1933 | return false; | |
1934 | ||
1935 | /* | |
1936 | * Migration allocates pages in the highest zone. If we cannot | |
1937 | * do so then migration (at least from node to node) is not | |
1938 | * possible. | |
1939 | */ | |
1940 | if (vma->vm_file && | |
1941 | gfp_zone(mapping_gfp_mask(vma->vm_file->f_mapping)) | |
1942 | < policy_zone) | |
1943 | return false; | |
1944 | return true; | |
1945 | } | |
1946 | ||
74d2c3a0 | 1947 | struct mempolicy *__get_vma_policy(struct vm_area_struct *vma, |
ddc1a5cb | 1948 | unsigned long addr, pgoff_t *ilx) |
1da177e4 | 1949 | { |
ddc1a5cb HD |
1950 | *ilx = 0; |
1951 | return (vma->vm_ops && vma->vm_ops->get_policy) ? | |
1952 | vma->vm_ops->get_policy(vma, addr, ilx) : vma->vm_policy; | |
74d2c3a0 ON |
1953 | } |
1954 | ||
1955 | /* | |
ddc1a5cb | 1956 | * get_vma_policy(@vma, @addr, @order, @ilx) |
74d2c3a0 ON |
1957 | * @vma: virtual memory area whose policy is sought |
1958 | * @addr: address in @vma for shared policy lookup | |
ddc1a5cb | 1959 | * @order: 0, or appropriate huge_page_order for interleaving |
fa3bea4e GP |
1960 | * @ilx: interleave index (output), for use only when MPOL_INTERLEAVE or |
1961 | * MPOL_WEIGHTED_INTERLEAVE | |
74d2c3a0 ON |
1962 | * |
1963 | * Returns effective policy for a VMA at specified address. | |
dd6eecb9 | 1964 | * Falls back to current->mempolicy or system default policy, as necessary. |
74d2c3a0 ON |
1965 | * Shared policies [those marked as MPOL_F_SHARED] require an extra reference |
1966 | * count--added by the get_policy() vm_op, as appropriate--to protect against | |
1967 | * freeing by another task. It is the caller's responsibility to free the | |
1968 | * extra reference for shared policies. | |
1969 | */ | |
ddc1a5cb HD |
1970 | struct mempolicy *get_vma_policy(struct vm_area_struct *vma, |
1971 | unsigned long addr, int order, pgoff_t *ilx) | |
74d2c3a0 | 1972 | { |
ddc1a5cb | 1973 | struct mempolicy *pol; |
74d2c3a0 | 1974 | |
ddc1a5cb | 1975 | pol = __get_vma_policy(vma, addr, ilx); |
8d90274b | 1976 | if (!pol) |
dd6eecb9 | 1977 | pol = get_task_policy(current); |
fa3bea4e GP |
1978 | if (pol->mode == MPOL_INTERLEAVE || |
1979 | pol->mode == MPOL_WEIGHTED_INTERLEAVE) { | |
ddc1a5cb HD |
1980 | *ilx += vma->vm_pgoff >> order; |
1981 | *ilx += (addr - vma->vm_start) >> (PAGE_SHIFT + order); | |
1982 | } | |
1da177e4 LT |
1983 | return pol; |
1984 | } | |
1985 | ||
6b6482bb | 1986 | bool vma_policy_mof(struct vm_area_struct *vma) |
fc314724 | 1987 | { |
6b6482bb | 1988 | struct mempolicy *pol; |
fc314724 | 1989 | |
6b6482bb ON |
1990 | if (vma->vm_ops && vma->vm_ops->get_policy) { |
1991 | bool ret = false; | |
ddc1a5cb | 1992 | pgoff_t ilx; /* ignored here */ |
fc314724 | 1993 | |
ddc1a5cb | 1994 | pol = vma->vm_ops->get_policy(vma, vma->vm_start, &ilx); |
6b6482bb ON |
1995 | if (pol && (pol->flags & MPOL_F_MOF)) |
1996 | ret = true; | |
1997 | mpol_cond_put(pol); | |
8d90274b | 1998 | |
6b6482bb | 1999 | return ret; |
fc314724 MG |
2000 | } |
2001 | ||
6b6482bb | 2002 | pol = vma->vm_policy; |
8d90274b | 2003 | if (!pol) |
6b6482bb | 2004 | pol = get_task_policy(current); |
8d90274b | 2005 | |
fc314724 MG |
2006 | return pol->flags & MPOL_F_MOF; |
2007 | } | |
2008 | ||
d2226ebd | 2009 | bool apply_policy_zone(struct mempolicy *policy, enum zone_type zone) |
d3eb1570 LJ |
2010 | { |
2011 | enum zone_type dynamic_policy_zone = policy_zone; | |
2012 | ||
2013 | BUG_ON(dynamic_policy_zone == ZONE_MOVABLE); | |
2014 | ||
2015 | /* | |
269fbe72 | 2016 | * if policy->nodes has movable memory only, |
d3eb1570 LJ |
2017 | * we apply policy when gfp_zone(gfp) = ZONE_MOVABLE only. |
2018 | * | |
269fbe72 | 2019 | * policy->nodes is intersect with node_states[N_MEMORY]. |
f0953a1b | 2020 | * so if the following test fails, it implies |
269fbe72 | 2021 | * policy->nodes has movable memory only. |
d3eb1570 | 2022 | */ |
269fbe72 | 2023 | if (!nodes_intersects(policy->nodes, node_states[N_HIGH_MEMORY])) |
d3eb1570 LJ |
2024 | dynamic_policy_zone = ZONE_MOVABLE; |
2025 | ||
2026 | return zone >= dynamic_policy_zone; | |
2027 | } | |
2028 | ||
fa3bea4e GP |
2029 | static unsigned int weighted_interleave_nodes(struct mempolicy *policy) |
2030 | { | |
274519ed GP |
2031 | unsigned int node; |
2032 | unsigned int cpuset_mems_cookie; | |
fa3bea4e | 2033 | |
274519ed GP |
2034 | retry: |
2035 | /* to prevent miscount use tsk->mems_allowed_seq to detect rebind */ | |
2036 | cpuset_mems_cookie = read_mems_allowed_begin(); | |
2037 | node = current->il_prev; | |
fa3bea4e GP |
2038 | if (!current->il_weight || !node_isset(node, policy->nodes)) { |
2039 | node = next_node_in(node, policy->nodes); | |
274519ed GP |
2040 | if (read_mems_allowed_retry(cpuset_mems_cookie)) |
2041 | goto retry; | |
fa3bea4e GP |
2042 | if (node == MAX_NUMNODES) |
2043 | return node; | |
2044 | current->il_prev = node; | |
2045 | current->il_weight = get_il_weight(node); | |
2046 | } | |
2047 | current->il_weight--; | |
2048 | return node; | |
2049 | } | |
2050 | ||
1da177e4 | 2051 | /* Do dynamic interleaving for a process */ |
c36f6e6d | 2052 | static unsigned int interleave_nodes(struct mempolicy *policy) |
1da177e4 | 2053 | { |
c36f6e6d | 2054 | unsigned int nid; |
274519ed GP |
2055 | unsigned int cpuset_mems_cookie; |
2056 | ||
2057 | /* to prevent miscount, use tsk->mems_allowed_seq to detect rebind */ | |
2058 | do { | |
2059 | cpuset_mems_cookie = read_mems_allowed_begin(); | |
2060 | nid = next_node_in(current->il_prev, policy->nodes); | |
2061 | } while (read_mems_allowed_retry(cpuset_mems_cookie)); | |
1da177e4 | 2062 | |
c36f6e6d HD |
2063 | if (nid < MAX_NUMNODES) |
2064 | current->il_prev = nid; | |
2065 | return nid; | |
1da177e4 LT |
2066 | } |
2067 | ||
dc85da15 CL |
2068 | /* |
2069 | * Depending on the memory policy provide a node from which to allocate the | |
2070 | * next slab entry. | |
2071 | */ | |
2a389610 | 2072 | unsigned int mempolicy_slab_node(void) |
dc85da15 | 2073 | { |
e7b691b0 | 2074 | struct mempolicy *policy; |
2a389610 | 2075 | int node = numa_mem_id(); |
e7b691b0 | 2076 | |
38b031dd | 2077 | if (!in_task()) |
2a389610 | 2078 | return node; |
e7b691b0 AK |
2079 | |
2080 | policy = current->mempolicy; | |
7858d7bc | 2081 | if (!policy) |
2a389610 | 2082 | return node; |
bea904d5 LS |
2083 | |
2084 | switch (policy->mode) { | |
2085 | case MPOL_PREFERRED: | |
269fbe72 | 2086 | return first_node(policy->nodes); |
765c4507 | 2087 | |
dc85da15 CL |
2088 | case MPOL_INTERLEAVE: |
2089 | return interleave_nodes(policy); | |
2090 | ||
fa3bea4e GP |
2091 | case MPOL_WEIGHTED_INTERLEAVE: |
2092 | return weighted_interleave_nodes(policy); | |
2093 | ||
b27abacc DH |
2094 | case MPOL_BIND: |
2095 | case MPOL_PREFERRED_MANY: | |
2096 | { | |
c33d6c06 MG |
2097 | struct zoneref *z; |
2098 | ||
dc85da15 CL |
2099 | /* |
2100 | * Follow bind policy behavior and start allocation at the | |
2101 | * first node. | |
2102 | */ | |
19770b32 | 2103 | struct zonelist *zonelist; |
19770b32 | 2104 | enum zone_type highest_zoneidx = gfp_zone(GFP_KERNEL); |
c9634cf0 | 2105 | zonelist = &NODE_DATA(node)->node_zonelists[ZONELIST_FALLBACK]; |
c33d6c06 | 2106 | z = first_zones_zonelist(zonelist, highest_zoneidx, |
269fbe72 | 2107 | &policy->nodes); |
29943248 | 2108 | return zonelist_zone(z) ? zonelist_node_idx(z) : node; |
dd1a239f | 2109 | } |
7858d7bc FT |
2110 | case MPOL_LOCAL: |
2111 | return node; | |
dc85da15 | 2112 | |
dc85da15 | 2113 | default: |
bea904d5 | 2114 | BUG(); |
dc85da15 CL |
2115 | } |
2116 | } | |
2117 | ||
9685e6e3 GP |
2118 | static unsigned int read_once_policy_nodemask(struct mempolicy *pol, |
2119 | nodemask_t *mask) | |
2120 | { | |
2121 | /* | |
2122 | * barrier stabilizes the nodemask locally so that it can be iterated | |
2123 | * over safely without concern for changes. Allocators validate node | |
2124 | * selection does not violate mems_allowed, so this is safe. | |
2125 | */ | |
2126 | barrier(); | |
2127 | memcpy(mask, &pol->nodes, sizeof(nodemask_t)); | |
2128 | barrier(); | |
2129 | return nodes_weight(*mask); | |
2130 | } | |
2131 | ||
fa3bea4e GP |
2132 | static unsigned int weighted_interleave_nid(struct mempolicy *pol, pgoff_t ilx) |
2133 | { | |
e341f9c3 | 2134 | struct weighted_interleave_state *state; |
fa3bea4e GP |
2135 | nodemask_t nodemask; |
2136 | unsigned int target, nr_nodes; | |
e341f9c3 | 2137 | u8 *table = NULL; |
fa3bea4e GP |
2138 | unsigned int weight_total = 0; |
2139 | u8 weight; | |
e341f9c3 | 2140 | int nid = 0; |
fa3bea4e GP |
2141 | |
2142 | nr_nodes = read_once_policy_nodemask(pol, &nodemask); | |
2143 | if (!nr_nodes) | |
2144 | return numa_node_id(); | |
2145 | ||
2146 | rcu_read_lock(); | |
e341f9c3 JH |
2147 | |
2148 | state = rcu_dereference(wi_state); | |
2149 | /* Uninitialized wi_state means we should assume all weights are 1 */ | |
2150 | if (state) | |
2151 | table = state->iw_table; | |
2152 | ||
fa3bea4e | 2153 | /* calculate the total weight */ |
e341f9c3 JH |
2154 | for_each_node_mask(nid, nodemask) |
2155 | weight_total += table ? table[nid] : 1; | |
fa3bea4e GP |
2156 | |
2157 | /* Calculate the node offset based on totals */ | |
2158 | target = ilx % weight_total; | |
2159 | nid = first_node(nodemask); | |
2160 | while (target) { | |
2161 | /* detect system default usage */ | |
2162 | weight = table ? table[nid] : 1; | |
fa3bea4e GP |
2163 | if (target < weight) |
2164 | break; | |
2165 | target -= weight; | |
2166 | nid = next_node_in(nid, nodemask); | |
2167 | } | |
2168 | rcu_read_unlock(); | |
2169 | return nid; | |
2170 | } | |
2171 | ||
fee83b3a | 2172 | /* |
ddc1a5cb HD |
2173 | * Do static interleaving for interleave index @ilx. Returns the ilx'th |
2174 | * node in pol->nodes (starting from ilx=0), wrapping around if ilx | |
2175 | * exceeds the number of present nodes. | |
fee83b3a | 2176 | */ |
ddc1a5cb | 2177 | static unsigned int interleave_nid(struct mempolicy *pol, pgoff_t ilx) |
1da177e4 | 2178 | { |
9685e6e3 | 2179 | nodemask_t nodemask; |
276aeee1 | 2180 | unsigned int target, nnodes; |
fee83b3a AM |
2181 | int i; |
2182 | int nid; | |
1da177e4 | 2183 | |
9685e6e3 | 2184 | nnodes = read_once_policy_nodemask(pol, &nodemask); |
f5b087b5 DR |
2185 | if (!nnodes) |
2186 | return numa_node_id(); | |
ddc1a5cb | 2187 | target = ilx % nnodes; |
276aeee1 | 2188 | nid = first_node(nodemask); |
fee83b3a | 2189 | for (i = 0; i < target; i++) |
276aeee1 | 2190 | nid = next_node(nid, nodemask); |
1da177e4 LT |
2191 | return nid; |
2192 | } | |
2193 | ||
ddc1a5cb HD |
2194 | /* |
2195 | * Return a nodemask representing a mempolicy for filtering nodes for | |
2196 | * page allocation, together with preferred node id (or the input node id). | |
2197 | */ | |
2198 | static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *pol, | |
2199 | pgoff_t ilx, int *nid) | |
5da7ca86 | 2200 | { |
ddc1a5cb | 2201 | nodemask_t *nodemask = NULL; |
5da7ca86 | 2202 | |
ddc1a5cb HD |
2203 | switch (pol->mode) { |
2204 | case MPOL_PREFERRED: | |
2205 | /* Override input node id */ | |
2206 | *nid = first_node(pol->nodes); | |
2207 | break; | |
2208 | case MPOL_PREFERRED_MANY: | |
2209 | nodemask = &pol->nodes; | |
2210 | if (pol->home_node != NUMA_NO_NODE) | |
2211 | *nid = pol->home_node; | |
2212 | break; | |
2213 | case MPOL_BIND: | |
2214 | /* Restrict to nodemask (but not on lower zones) */ | |
2215 | if (apply_policy_zone(pol, gfp_zone(gfp)) && | |
2216 | cpuset_nodemask_valid_mems_allowed(&pol->nodes)) | |
2217 | nodemask = &pol->nodes; | |
2218 | if (pol->home_node != NUMA_NO_NODE) | |
2219 | *nid = pol->home_node; | |
3b98b087 | 2220 | /* |
ddc1a5cb HD |
2221 | * __GFP_THISNODE shouldn't even be used with the bind policy |
2222 | * because we might easily break the expectation to stay on the | |
2223 | * requested node and not break the policy. | |
3b98b087 | 2224 | */ |
ddc1a5cb HD |
2225 | WARN_ON_ONCE(gfp & __GFP_THISNODE); |
2226 | break; | |
2227 | case MPOL_INTERLEAVE: | |
2228 | /* Override input node id */ | |
2229 | *nid = (ilx == NO_INTERLEAVE_INDEX) ? | |
2230 | interleave_nodes(pol) : interleave_nid(pol, ilx); | |
2231 | break; | |
fa3bea4e GP |
2232 | case MPOL_WEIGHTED_INTERLEAVE: |
2233 | *nid = (ilx == NO_INTERLEAVE_INDEX) ? | |
2234 | weighted_interleave_nodes(pol) : | |
2235 | weighted_interleave_nid(pol, ilx); | |
2236 | break; | |
ddc1a5cb HD |
2237 | } |
2238 | ||
2239 | return nodemask; | |
5da7ca86 CL |
2240 | } |
2241 | ||
00ac59ad | 2242 | #ifdef CONFIG_HUGETLBFS |
480eccf9 | 2243 | /* |
04ec6264 | 2244 | * huge_node(@vma, @addr, @gfp_flags, @mpol) |
b46e14ac FF |
2245 | * @vma: virtual memory area whose policy is sought |
2246 | * @addr: address in @vma for shared policy lookup and interleave policy | |
2247 | * @gfp_flags: for requested zone | |
2248 | * @mpol: pointer to mempolicy pointer for reference counted mempolicy | |
b27abacc | 2249 | * @nodemask: pointer to nodemask pointer for 'bind' and 'prefer-many' policy |
480eccf9 | 2250 | * |
04ec6264 | 2251 | * Returns a nid suitable for a huge page allocation and a pointer |
52cd3b07 | 2252 | * to the struct mempolicy for conditional unref after allocation. |
b27abacc DH |
2253 | * If the effective policy is 'bind' or 'prefer-many', returns a pointer |
2254 | * to the mempolicy's @nodemask for filtering the zonelist. | |
480eccf9 | 2255 | */ |
04ec6264 | 2256 | int huge_node(struct vm_area_struct *vma, unsigned long addr, gfp_t gfp_flags, |
ddc1a5cb | 2257 | struct mempolicy **mpol, nodemask_t **nodemask) |
5da7ca86 | 2258 | { |
ddc1a5cb | 2259 | pgoff_t ilx; |
04ec6264 | 2260 | int nid; |
5da7ca86 | 2261 | |
ddc1a5cb HD |
2262 | nid = numa_node_id(); |
2263 | *mpol = get_vma_policy(vma, addr, hstate_vma(vma)->order, &ilx); | |
2264 | *nodemask = policy_nodemask(gfp_flags, *mpol, ilx, &nid); | |
04ec6264 | 2265 | return nid; |
5da7ca86 | 2266 | } |
06808b08 LS |
2267 | |
2268 | /* | |
2269 | * init_nodemask_of_mempolicy | |
2270 | * | |
2271 | * If the current task's mempolicy is "default" [NULL], return 'false' | |
2272 | * to indicate default policy. Otherwise, extract the policy nodemask | |
2273 | * for 'bind' or 'interleave' policy into the argument nodemask, or | |
2274 | * initialize the argument nodemask to contain the single node for | |
2275 | * 'preferred' or 'local' policy and return 'true' to indicate presence | |
2276 | * of non-default mempolicy. | |
2277 | * | |
2278 | * We don't bother with reference counting the mempolicy [mpol_get/put] | |
2279 | * because the current task is examining it's own mempolicy and a task's | |
2280 | * mempolicy is only ever changed by the task itself. | |
2281 | * | |
2282 | * N.B., it is the caller's responsibility to free a returned nodemask. | |
2283 | */ | |
2284 | bool init_nodemask_of_mempolicy(nodemask_t *mask) | |
2285 | { | |
2286 | struct mempolicy *mempolicy; | |
06808b08 LS |
2287 | |
2288 | if (!(mask && current->mempolicy)) | |
2289 | return false; | |
2290 | ||
c0ff7453 | 2291 | task_lock(current); |
06808b08 LS |
2292 | mempolicy = current->mempolicy; |
2293 | switch (mempolicy->mode) { | |
2294 | case MPOL_PREFERRED: | |
b27abacc | 2295 | case MPOL_PREFERRED_MANY: |
06808b08 | 2296 | case MPOL_BIND: |
06808b08 | 2297 | case MPOL_INTERLEAVE: |
fa3bea4e | 2298 | case MPOL_WEIGHTED_INTERLEAVE: |
269fbe72 | 2299 | *mask = mempolicy->nodes; |
7858d7bc FT |
2300 | break; |
2301 | ||
2302 | case MPOL_LOCAL: | |
269fbe72 | 2303 | init_nodemask_of_node(mask, numa_node_id()); |
06808b08 LS |
2304 | break; |
2305 | ||
2306 | default: | |
2307 | BUG(); | |
2308 | } | |
c0ff7453 | 2309 | task_unlock(current); |
06808b08 LS |
2310 | |
2311 | return true; | |
2312 | } | |
00ac59ad | 2313 | #endif |
5da7ca86 | 2314 | |
6f48d0eb | 2315 | /* |
b26e517a | 2316 | * mempolicy_in_oom_domain |
6f48d0eb | 2317 | * |
b26e517a FT |
2318 | * If tsk's mempolicy is "bind", check for intersection between mask and |
2319 | * the policy nodemask. Otherwise, return true for all other policies | |
2320 | * including "interleave", as a tsk with "interleave" policy may have | |
2321 | * memory allocated from all nodes in system. | |
6f48d0eb DR |
2322 | * |
2323 | * Takes task_lock(tsk) to prevent freeing of its mempolicy. | |
2324 | */ | |
b26e517a | 2325 | bool mempolicy_in_oom_domain(struct task_struct *tsk, |
6f48d0eb DR |
2326 | const nodemask_t *mask) |
2327 | { | |
2328 | struct mempolicy *mempolicy; | |
2329 | bool ret = true; | |
2330 | ||
2331 | if (!mask) | |
2332 | return ret; | |
b26e517a | 2333 | |
6f48d0eb DR |
2334 | task_lock(tsk); |
2335 | mempolicy = tsk->mempolicy; | |
b26e517a | 2336 | if (mempolicy && mempolicy->mode == MPOL_BIND) |
269fbe72 | 2337 | ret = nodes_intersects(mempolicy->nodes, *mask); |
6f48d0eb | 2338 | task_unlock(tsk); |
b26e517a | 2339 | |
6f48d0eb DR |
2340 | return ret; |
2341 | } | |
2342 | ||
4c54d949 | 2343 | static struct page *alloc_pages_preferred_many(gfp_t gfp, unsigned int order, |
ddc1a5cb | 2344 | int nid, nodemask_t *nodemask) |
4c54d949 FT |
2345 | { |
2346 | struct page *page; | |
2347 | gfp_t preferred_gfp; | |
2348 | ||
2349 | /* | |
2350 | * This is a two pass approach. The first pass will only try the | |
2351 | * preferred nodes but skip the direct reclaim and allow the | |
2352 | * allocation to fail, while the second pass will try all the | |
2353 | * nodes in system. | |
2354 | */ | |
2355 | preferred_gfp = gfp | __GFP_NOWARN; | |
2356 | preferred_gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); | |
64297524 | 2357 | page = __alloc_frozen_pages_noprof(preferred_gfp, order, nid, nodemask); |
4c54d949 | 2358 | if (!page) |
64297524 | 2359 | page = __alloc_frozen_pages_noprof(gfp, order, nid, NULL); |
4c54d949 FT |
2360 | |
2361 | return page; | |
2362 | } | |
2363 | ||
1da177e4 | 2364 | /** |
ddc1a5cb | 2365 | * alloc_pages_mpol - Allocate pages according to NUMA mempolicy. |
eb350739 | 2366 | * @gfp: GFP flags. |
ddc1a5cb HD |
2367 | * @order: Order of the page allocation. |
2368 | * @pol: Pointer to the NUMA mempolicy. | |
2369 | * @ilx: Index for interleave mempolicy (also distinguishes alloc_pages()). | |
2370 | * @nid: Preferred node (usually numa_node_id() but @mpol may override it). | |
1da177e4 | 2371 | * |
ddc1a5cb | 2372 | * Return: The page on success or NULL if allocation fails. |
1da177e4 | 2373 | */ |
38558b24 | 2374 | static struct page *alloc_pages_mpol(gfp_t gfp, unsigned int order, |
ddc1a5cb | 2375 | struct mempolicy *pol, pgoff_t ilx, int nid) |
1da177e4 | 2376 | { |
ddc1a5cb HD |
2377 | nodemask_t *nodemask; |
2378 | struct page *page; | |
adf88aa8 | 2379 | |
ddc1a5cb | 2380 | nodemask = policy_nodemask(gfp, pol, ilx, &nid); |
4c54d949 | 2381 | |
ddc1a5cb HD |
2382 | if (pol->mode == MPOL_PREFERRED_MANY) |
2383 | return alloc_pages_preferred_many(gfp, order, nid, nodemask); | |
19deb769 | 2384 | |
ddc1a5cb HD |
2385 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && |
2386 | /* filter "hugepage" allocation, unless from alloc_pages() */ | |
2387 | order == HPAGE_PMD_ORDER && ilx != NO_INTERLEAVE_INDEX) { | |
19deb769 DR |
2388 | /* |
2389 | * For hugepage allocation and non-interleave policy which | |
2390 | * allows the current node (or other explicitly preferred | |
2391 | * node) we only try to allocate from the current/preferred | |
2392 | * node and don't fall back to other nodes, as the cost of | |
2393 | * remote accesses would likely offset THP benefits. | |
2394 | * | |
b27abacc | 2395 | * If the policy is interleave or does not allow the current |
19deb769 DR |
2396 | * node in its nodemask, we allocate the standard way. |
2397 | */ | |
ddc1a5cb | 2398 | if (pol->mode != MPOL_INTERLEAVE && |
fa3bea4e | 2399 | pol->mode != MPOL_WEIGHTED_INTERLEAVE && |
ddc1a5cb | 2400 | (!nodemask || node_isset(nid, *nodemask))) { |
cc638f32 VB |
2401 | /* |
2402 | * First, try to allocate THP only on local node, but | |
2403 | * don't reclaim unnecessarily, just compact. | |
2404 | */ | |
64297524 MWO |
2405 | page = __alloc_frozen_pages_noprof( |
2406 | gfp | __GFP_THISNODE | __GFP_NORETRY, order, | |
2407 | nid, NULL); | |
ddc1a5cb HD |
2408 | if (page || !(gfp & __GFP_DIRECT_RECLAIM)) |
2409 | return page; | |
76e654cc DR |
2410 | /* |
2411 | * If hugepage allocations are configured to always | |
2412 | * synchronous compact or the vma has been madvised | |
2413 | * to prefer hugepage backing, retry allowing remote | |
cc638f32 | 2414 | * memory with both reclaim and compact as well. |
76e654cc | 2415 | */ |
ddc1a5cb HD |
2416 | } |
2417 | } | |
76e654cc | 2418 | |
64297524 | 2419 | page = __alloc_frozen_pages_noprof(gfp, order, nid, nodemask); |
ddc1a5cb | 2420 | |
264a88ca HK |
2421 | if (unlikely(pol->mode == MPOL_INTERLEAVE || |
2422 | pol->mode == MPOL_WEIGHTED_INTERLEAVE) && page) { | |
ddc1a5cb HD |
2423 | /* skip NUMA_INTERLEAVE_HIT update if numa stats is disabled */ |
2424 | if (static_branch_likely(&vm_numa_stat_key) && | |
2425 | page_to_nid(page) == nid) { | |
2426 | preempt_disable(); | |
2427 | __count_numa_event(page_zone(page), NUMA_INTERLEAVE_HIT); | |
2428 | preempt_enable(); | |
19deb769 | 2429 | } |
356ff8a9 DR |
2430 | } |
2431 | ||
ddc1a5cb HD |
2432 | return page; |
2433 | } | |
2434 | ||
a19621ed KW |
2435 | struct folio *folio_alloc_mpol_noprof(gfp_t gfp, unsigned int order, |
2436 | struct mempolicy *pol, pgoff_t ilx, int nid) | |
2437 | { | |
64297524 MWO |
2438 | struct page *page = alloc_pages_mpol(gfp | __GFP_COMP, order, pol, |
2439 | ilx, nid); | |
2440 | if (!page) | |
2441 | return NULL; | |
2442 | ||
2443 | set_page_refcounted(page); | |
2444 | return page_rmappable_folio(page); | |
a19621ed KW |
2445 | } |
2446 | ||
ddc1a5cb HD |
2447 | /** |
2448 | * vma_alloc_folio - Allocate a folio for a VMA. | |
2449 | * @gfp: GFP flags. | |
2450 | * @order: Order of the folio. | |
2451 | * @vma: Pointer to VMA. | |
2452 | * @addr: Virtual address of the allocation. Must be inside @vma. | |
ddc1a5cb HD |
2453 | * |
2454 | * Allocate a folio for a specific address in @vma, using the appropriate | |
2455 | * NUMA policy. The caller must hold the mmap_lock of the mm_struct of the | |
2456 | * VMA to prevent it from going away. Should be used for all allocations | |
2457 | * for folios that will be mapped into user space, excepting hugetlbfs, and | |
38558b24 | 2458 | * excepting where direct use of folio_alloc_mpol() is more appropriate. |
ddc1a5cb HD |
2459 | * |
2460 | * Return: The folio on success or NULL if allocation fails. | |
2461 | */ | |
b951aaff | 2462 | struct folio *vma_alloc_folio_noprof(gfp_t gfp, int order, struct vm_area_struct *vma, |
6359c39c | 2463 | unsigned long addr) |
ddc1a5cb HD |
2464 | { |
2465 | struct mempolicy *pol; | |
2466 | pgoff_t ilx; | |
3174d70c | 2467 | struct folio *folio; |
ddc1a5cb | 2468 | |
9651fced JD |
2469 | if (vma->vm_flags & VM_DROPPABLE) |
2470 | gfp |= __GFP_NOWARN; | |
2471 | ||
ddc1a5cb | 2472 | pol = get_vma_policy(vma, addr, order, &ilx); |
3174d70c | 2473 | folio = folio_alloc_mpol_noprof(gfp, order, pol, ilx, numa_node_id()); |
d51e9894 | 2474 | mpol_cond_put(pol); |
3174d70c | 2475 | return folio; |
f584b680 | 2476 | } |
b951aaff | 2477 | EXPORT_SYMBOL(vma_alloc_folio_noprof); |
f584b680 | 2478 | |
64297524 MWO |
2479 | struct page *alloc_frozen_pages_noprof(gfp_t gfp, unsigned order) |
2480 | { | |
2481 | struct mempolicy *pol = &default_policy; | |
2482 | ||
2483 | /* | |
2484 | * No reference counting needed for current->mempolicy | |
2485 | * nor system default_policy | |
2486 | */ | |
2487 | if (!in_interrupt() && !(gfp & __GFP_THISNODE)) | |
2488 | pol = get_task_policy(current); | |
2489 | ||
2490 | return alloc_pages_mpol(gfp, order, pol, NO_INTERLEAVE_INDEX, | |
2491 | numa_node_id()); | |
2492 | } | |
2493 | ||
1da177e4 | 2494 | /** |
6421ec76 MWO |
2495 | * alloc_pages - Allocate pages. |
2496 | * @gfp: GFP flags. | |
2497 | * @order: Power of two of number of pages to allocate. | |
1da177e4 | 2498 | * |
6421ec76 MWO |
2499 | * Allocate 1 << @order contiguous pages. The physical address of the |
2500 | * first page is naturally aligned (eg an order-3 allocation will be aligned | |
2501 | * to a multiple of 8 * PAGE_SIZE bytes). The NUMA policy of the current | |
2502 | * process is honoured when in process context. | |
1da177e4 | 2503 | * |
6421ec76 MWO |
2504 | * Context: Can be called from any context, providing the appropriate GFP |
2505 | * flags are used. | |
2506 | * Return: The page on success or NULL if allocation fails. | |
1da177e4 | 2507 | */ |
b951aaff | 2508 | struct page *alloc_pages_noprof(gfp_t gfp, unsigned int order) |
1da177e4 | 2509 | { |
64297524 | 2510 | struct page *page = alloc_frozen_pages_noprof(gfp, order); |
cc9a6c87 | 2511 | |
64297524 MWO |
2512 | if (page) |
2513 | set_page_refcounted(page); | |
2514 | return page; | |
1da177e4 | 2515 | } |
b951aaff | 2516 | EXPORT_SYMBOL(alloc_pages_noprof); |
1da177e4 | 2517 | |
b951aaff | 2518 | struct folio *folio_alloc_noprof(gfp_t gfp, unsigned int order) |
cc09cb13 | 2519 | { |
b951aaff | 2520 | return page_rmappable_folio(alloc_pages_noprof(gfp | __GFP_COMP, order)); |
cc09cb13 | 2521 | } |
b951aaff | 2522 | EXPORT_SYMBOL(folio_alloc_noprof); |
cc09cb13 | 2523 | |
6bf9b5b4 | 2524 | static unsigned long alloc_pages_bulk_interleave(gfp_t gfp, |
c00b6b96 CW |
2525 | struct mempolicy *pol, unsigned long nr_pages, |
2526 | struct page **page_array) | |
2527 | { | |
2528 | int nodes; | |
2529 | unsigned long nr_pages_per_node; | |
2530 | int delta; | |
2531 | int i; | |
2532 | unsigned long nr_allocated; | |
2533 | unsigned long total_allocated = 0; | |
2534 | ||
2535 | nodes = nodes_weight(pol->nodes); | |
2536 | nr_pages_per_node = nr_pages / nodes; | |
2537 | delta = nr_pages - nodes * nr_pages_per_node; | |
2538 | ||
2539 | for (i = 0; i < nodes; i++) { | |
2540 | if (delta) { | |
b951aaff | 2541 | nr_allocated = alloc_pages_bulk_noprof(gfp, |
c00b6b96 | 2542 | interleave_nodes(pol), NULL, |
c8b97953 | 2543 | nr_pages_per_node + 1, |
c00b6b96 CW |
2544 | page_array); |
2545 | delta--; | |
2546 | } else { | |
b951aaff | 2547 | nr_allocated = alloc_pages_bulk_noprof(gfp, |
c00b6b96 | 2548 | interleave_nodes(pol), NULL, |
c8b97953 | 2549 | nr_pages_per_node, page_array); |
c00b6b96 CW |
2550 | } |
2551 | ||
2552 | page_array += nr_allocated; | |
2553 | total_allocated += nr_allocated; | |
2554 | } | |
2555 | ||
2556 | return total_allocated; | |
2557 | } | |
2558 | ||
6bf9b5b4 | 2559 | static unsigned long alloc_pages_bulk_weighted_interleave(gfp_t gfp, |
fa3bea4e GP |
2560 | struct mempolicy *pol, unsigned long nr_pages, |
2561 | struct page **page_array) | |
2562 | { | |
e341f9c3 | 2563 | struct weighted_interleave_state *state; |
fa3bea4e | 2564 | struct task_struct *me = current; |
274519ed | 2565 | unsigned int cpuset_mems_cookie; |
fa3bea4e GP |
2566 | unsigned long total_allocated = 0; |
2567 | unsigned long nr_allocated = 0; | |
2568 | unsigned long rounds; | |
2569 | unsigned long node_pages, delta; | |
e341f9c3 | 2570 | u8 *weights, weight; |
fa3bea4e GP |
2571 | unsigned int weight_total = 0; |
2572 | unsigned long rem_pages = nr_pages; | |
2573 | nodemask_t nodes; | |
2574 | int nnodes, node; | |
2575 | int resume_node = MAX_NUMNODES - 1; | |
2576 | u8 resume_weight = 0; | |
2577 | int prev_node; | |
2578 | int i; | |
2579 | ||
2580 | if (!nr_pages) | |
2581 | return 0; | |
2582 | ||
274519ed GP |
2583 | /* read the nodes onto the stack, retry if done during rebind */ |
2584 | do { | |
2585 | cpuset_mems_cookie = read_mems_allowed_begin(); | |
2586 | nnodes = read_once_policy_nodemask(pol, &nodes); | |
2587 | } while (read_mems_allowed_retry(cpuset_mems_cookie)); | |
2588 | ||
2589 | /* if the nodemask has become invalid, we cannot do anything */ | |
fa3bea4e GP |
2590 | if (!nnodes) |
2591 | return 0; | |
2592 | ||
2593 | /* Continue allocating from most recent node and adjust the nr_pages */ | |
2594 | node = me->il_prev; | |
2595 | weight = me->il_weight; | |
2596 | if (weight && node_isset(node, nodes)) { | |
2597 | node_pages = min(rem_pages, weight); | |
2598 | nr_allocated = __alloc_pages_bulk(gfp, node, NULL, node_pages, | |
c8b97953 | 2599 | page_array); |
fa3bea4e GP |
2600 | page_array += nr_allocated; |
2601 | total_allocated += nr_allocated; | |
2602 | /* if that's all the pages, no need to interleave */ | |
2603 | if (rem_pages <= weight) { | |
2604 | me->il_weight -= rem_pages; | |
2605 | return total_allocated; | |
2606 | } | |
2607 | /* Otherwise we adjust remaining pages, continue from there */ | |
2608 | rem_pages -= weight; | |
2609 | } | |
2610 | /* clear active weight in case of an allocation failure */ | |
2611 | me->il_weight = 0; | |
2612 | prev_node = node; | |
2613 | ||
2614 | /* create a local copy of node weights to operate on outside rcu */ | |
2615 | weights = kzalloc(nr_node_ids, GFP_KERNEL); | |
2616 | if (!weights) | |
2617 | return total_allocated; | |
2618 | ||
2619 | rcu_read_lock(); | |
e341f9c3 JH |
2620 | state = rcu_dereference(wi_state); |
2621 | if (state) { | |
2622 | memcpy(weights, state->iw_table, nr_node_ids * sizeof(u8)); | |
2623 | rcu_read_unlock(); | |
2624 | } else { | |
2625 | rcu_read_unlock(); | |
2626 | for (i = 0; i < nr_node_ids; i++) | |
2627 | weights[i] = 1; | |
2628 | } | |
fa3bea4e GP |
2629 | |
2630 | /* calculate total, detect system default usage */ | |
e341f9c3 | 2631 | for_each_node_mask(node, nodes) |
fa3bea4e | 2632 | weight_total += weights[node]; |
fa3bea4e GP |
2633 | |
2634 | /* | |
2635 | * Calculate rounds/partial rounds to minimize __alloc_pages_bulk calls. | |
2636 | * Track which node weighted interleave should resume from. | |
2637 | * | |
2638 | * if (rounds > 0) and (delta == 0), resume_node will always be | |
2639 | * the node following prev_node and its weight. | |
2640 | */ | |
2641 | rounds = rem_pages / weight_total; | |
2642 | delta = rem_pages % weight_total; | |
2643 | resume_node = next_node_in(prev_node, nodes); | |
2644 | resume_weight = weights[resume_node]; | |
2645 | for (i = 0; i < nnodes; i++) { | |
2646 | node = next_node_in(prev_node, nodes); | |
2647 | weight = weights[node]; | |
2648 | node_pages = weight * rounds; | |
2649 | /* If a delta exists, add this node's portion of the delta */ | |
2650 | if (delta > weight) { | |
2651 | node_pages += weight; | |
2652 | delta -= weight; | |
2653 | } else if (delta) { | |
2654 | /* when delta is depleted, resume from that node */ | |
2655 | node_pages += delta; | |
2656 | resume_node = node; | |
2657 | resume_weight = weight - delta; | |
2658 | delta = 0; | |
2659 | } | |
2660 | /* node_pages can be 0 if an allocation fails and rounds == 0 */ | |
2661 | if (!node_pages) | |
2662 | break; | |
2663 | nr_allocated = __alloc_pages_bulk(gfp, node, NULL, node_pages, | |
c8b97953 | 2664 | page_array); |
fa3bea4e GP |
2665 | page_array += nr_allocated; |
2666 | total_allocated += nr_allocated; | |
2667 | if (total_allocated == nr_pages) | |
2668 | break; | |
2669 | prev_node = node; | |
2670 | } | |
2671 | me->il_prev = resume_node; | |
2672 | me->il_weight = resume_weight; | |
2673 | kfree(weights); | |
2674 | return total_allocated; | |
2675 | } | |
2676 | ||
6bf9b5b4 | 2677 | static unsigned long alloc_pages_bulk_preferred_many(gfp_t gfp, int nid, |
c00b6b96 CW |
2678 | struct mempolicy *pol, unsigned long nr_pages, |
2679 | struct page **page_array) | |
2680 | { | |
2681 | gfp_t preferred_gfp; | |
2682 | unsigned long nr_allocated = 0; | |
2683 | ||
2684 | preferred_gfp = gfp | __GFP_NOWARN; | |
2685 | preferred_gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); | |
2686 | ||
b951aaff | 2687 | nr_allocated = alloc_pages_bulk_noprof(preferred_gfp, nid, &pol->nodes, |
c8b97953 | 2688 | nr_pages, page_array); |
c00b6b96 CW |
2689 | |
2690 | if (nr_allocated < nr_pages) | |
b951aaff | 2691 | nr_allocated += alloc_pages_bulk_noprof(gfp, numa_node_id(), NULL, |
c8b97953 | 2692 | nr_pages - nr_allocated, |
c00b6b96 CW |
2693 | page_array + nr_allocated); |
2694 | return nr_allocated; | |
2695 | } | |
2696 | ||
2697 | /* alloc pages bulk and mempolicy should be considered at the | |
2698 | * same time in some situation such as vmalloc. | |
2699 | * | |
2700 | * It can accelerate memory allocation especially interleaving | |
2701 | * allocate memory. | |
2702 | */ | |
6bf9b5b4 | 2703 | unsigned long alloc_pages_bulk_mempolicy_noprof(gfp_t gfp, |
c00b6b96 CW |
2704 | unsigned long nr_pages, struct page **page_array) |
2705 | { | |
2706 | struct mempolicy *pol = &default_policy; | |
ddc1a5cb HD |
2707 | nodemask_t *nodemask; |
2708 | int nid; | |
c00b6b96 CW |
2709 | |
2710 | if (!in_interrupt() && !(gfp & __GFP_THISNODE)) | |
2711 | pol = get_task_policy(current); | |
2712 | ||
2713 | if (pol->mode == MPOL_INTERLEAVE) | |
6bf9b5b4 | 2714 | return alloc_pages_bulk_interleave(gfp, pol, |
c00b6b96 CW |
2715 | nr_pages, page_array); |
2716 | ||
fa3bea4e | 2717 | if (pol->mode == MPOL_WEIGHTED_INTERLEAVE) |
6bf9b5b4 | 2718 | return alloc_pages_bulk_weighted_interleave( |
fa3bea4e GP |
2719 | gfp, pol, nr_pages, page_array); |
2720 | ||
c00b6b96 | 2721 | if (pol->mode == MPOL_PREFERRED_MANY) |
6bf9b5b4 | 2722 | return alloc_pages_bulk_preferred_many(gfp, |
c00b6b96 CW |
2723 | numa_node_id(), pol, nr_pages, page_array); |
2724 | ||
ddc1a5cb HD |
2725 | nid = numa_node_id(); |
2726 | nodemask = policy_nodemask(gfp, pol, NO_INTERLEAVE_INDEX, &nid); | |
b951aaff | 2727 | return alloc_pages_bulk_noprof(gfp, nid, nodemask, |
c8b97953 | 2728 | nr_pages, page_array); |
c00b6b96 CW |
2729 | } |
2730 | ||
ef0855d3 ON |
2731 | int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst) |
2732 | { | |
c36f6e6d | 2733 | struct mempolicy *pol = mpol_dup(src->vm_policy); |
ef0855d3 ON |
2734 | |
2735 | if (IS_ERR(pol)) | |
2736 | return PTR_ERR(pol); | |
2737 | dst->vm_policy = pol; | |
2738 | return 0; | |
2739 | } | |
2740 | ||
4225399a | 2741 | /* |
846a16bf | 2742 | * If mpol_dup() sees current->cpuset == cpuset_being_rebound, then it |
4225399a PJ |
2743 | * rebinds the mempolicy its copying by calling mpol_rebind_policy() |
2744 | * with the mems_allowed returned by cpuset_mems_allowed(). This | |
2745 | * keeps mempolicies cpuset relative after its cpuset moves. See | |
2746 | * further kernel/cpuset.c update_nodemask(). | |
708c1bbc MX |
2747 | * |
2748 | * current's mempolicy may be rebinded by the other task(the task that changes | |
2749 | * cpuset's mems), so we needn't do rebind work for current task. | |
4225399a | 2750 | */ |
4225399a | 2751 | |
846a16bf LS |
2752 | /* Slow path of a mempolicy duplicate */ |
2753 | struct mempolicy *__mpol_dup(struct mempolicy *old) | |
1da177e4 LT |
2754 | { |
2755 | struct mempolicy *new = kmem_cache_alloc(policy_cache, GFP_KERNEL); | |
2756 | ||
2757 | if (!new) | |
2758 | return ERR_PTR(-ENOMEM); | |
708c1bbc MX |
2759 | |
2760 | /* task's mempolicy is protected by alloc_lock */ | |
2761 | if (old == current->mempolicy) { | |
2762 | task_lock(current); | |
2763 | *new = *old; | |
2764 | task_unlock(current); | |
2765 | } else | |
2766 | *new = *old; | |
2767 | ||
4225399a PJ |
2768 | if (current_cpuset_is_being_rebound()) { |
2769 | nodemask_t mems = cpuset_mems_allowed(current); | |
213980c0 | 2770 | mpol_rebind_policy(new, &mems); |
4225399a | 2771 | } |
1da177e4 | 2772 | atomic_set(&new->refcnt, 1); |
1da177e4 LT |
2773 | return new; |
2774 | } | |
2775 | ||
2776 | /* Slow path of a mempolicy comparison */ | |
fcfb4dcc | 2777 | bool __mpol_equal(struct mempolicy *a, struct mempolicy *b) |
1da177e4 LT |
2778 | { |
2779 | if (!a || !b) | |
fcfb4dcc | 2780 | return false; |
45c4745a | 2781 | if (a->mode != b->mode) |
fcfb4dcc | 2782 | return false; |
19800502 | 2783 | if (a->flags != b->flags) |
fcfb4dcc | 2784 | return false; |
c6018b4b AK |
2785 | if (a->home_node != b->home_node) |
2786 | return false; | |
19800502 BL |
2787 | if (mpol_store_user_nodemask(a)) |
2788 | if (!nodes_equal(a->w.user_nodemask, b->w.user_nodemask)) | |
fcfb4dcc | 2789 | return false; |
19800502 | 2790 | |
45c4745a | 2791 | switch (a->mode) { |
19770b32 | 2792 | case MPOL_BIND: |
1da177e4 | 2793 | case MPOL_INTERLEAVE: |
1da177e4 | 2794 | case MPOL_PREFERRED: |
b27abacc | 2795 | case MPOL_PREFERRED_MANY: |
fa3bea4e | 2796 | case MPOL_WEIGHTED_INTERLEAVE: |
269fbe72 | 2797 | return !!nodes_equal(a->nodes, b->nodes); |
7858d7bc FT |
2798 | case MPOL_LOCAL: |
2799 | return true; | |
1da177e4 LT |
2800 | default: |
2801 | BUG(); | |
fcfb4dcc | 2802 | return false; |
1da177e4 LT |
2803 | } |
2804 | } | |
2805 | ||
1da177e4 LT |
2806 | /* |
2807 | * Shared memory backing store policy support. | |
2808 | * | |
2809 | * Remember policies even when nobody has shared memory mapped. | |
2810 | * The policies are kept in Red-Black tree linked from the inode. | |
4a8c7bb5 | 2811 | * They are protected by the sp->lock rwlock, which should be held |
1da177e4 LT |
2812 | * for any accesses to the tree. |
2813 | */ | |
2814 | ||
4a8c7bb5 NZ |
2815 | /* |
2816 | * lookup first element intersecting start-end. Caller holds sp->lock for | |
2817 | * reading or for writing | |
2818 | */ | |
93397c3b HD |
2819 | static struct sp_node *sp_lookup(struct shared_policy *sp, |
2820 | pgoff_t start, pgoff_t end) | |
1da177e4 LT |
2821 | { |
2822 | struct rb_node *n = sp->root.rb_node; | |
2823 | ||
2824 | while (n) { | |
2825 | struct sp_node *p = rb_entry(n, struct sp_node, nd); | |
2826 | ||
2827 | if (start >= p->end) | |
2828 | n = n->rb_right; | |
2829 | else if (end <= p->start) | |
2830 | n = n->rb_left; | |
2831 | else | |
2832 | break; | |
2833 | } | |
2834 | if (!n) | |
2835 | return NULL; | |
2836 | for (;;) { | |
2837 | struct sp_node *w = NULL; | |
2838 | struct rb_node *prev = rb_prev(n); | |
2839 | if (!prev) | |
2840 | break; | |
2841 | w = rb_entry(prev, struct sp_node, nd); | |
2842 | if (w->end <= start) | |
2843 | break; | |
2844 | n = prev; | |
2845 | } | |
2846 | return rb_entry(n, struct sp_node, nd); | |
2847 | } | |
2848 | ||
4a8c7bb5 NZ |
2849 | /* |
2850 | * Insert a new shared policy into the list. Caller holds sp->lock for | |
2851 | * writing. | |
2852 | */ | |
1da177e4 LT |
2853 | static void sp_insert(struct shared_policy *sp, struct sp_node *new) |
2854 | { | |
2855 | struct rb_node **p = &sp->root.rb_node; | |
2856 | struct rb_node *parent = NULL; | |
2857 | struct sp_node *nd; | |
2858 | ||
2859 | while (*p) { | |
2860 | parent = *p; | |
2861 | nd = rb_entry(parent, struct sp_node, nd); | |
2862 | if (new->start < nd->start) | |
2863 | p = &(*p)->rb_left; | |
2864 | else if (new->end > nd->end) | |
2865 | p = &(*p)->rb_right; | |
2866 | else | |
2867 | BUG(); | |
2868 | } | |
2869 | rb_link_node(&new->nd, parent, p); | |
2870 | rb_insert_color(&new->nd, &sp->root); | |
1da177e4 LT |
2871 | } |
2872 | ||
2873 | /* Find shared policy intersecting idx */ | |
93397c3b HD |
2874 | struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp, |
2875 | pgoff_t idx) | |
1da177e4 LT |
2876 | { |
2877 | struct mempolicy *pol = NULL; | |
2878 | struct sp_node *sn; | |
2879 | ||
2880 | if (!sp->root.rb_node) | |
2881 | return NULL; | |
4a8c7bb5 | 2882 | read_lock(&sp->lock); |
1da177e4 LT |
2883 | sn = sp_lookup(sp, idx, idx+1); |
2884 | if (sn) { | |
2885 | mpol_get(sn->policy); | |
2886 | pol = sn->policy; | |
2887 | } | |
4a8c7bb5 | 2888 | read_unlock(&sp->lock); |
1da177e4 LT |
2889 | return pol; |
2890 | } | |
2891 | ||
63f74ca2 KM |
2892 | static void sp_free(struct sp_node *n) |
2893 | { | |
2894 | mpol_put(n->policy); | |
2895 | kmem_cache_free(sn_cache, n); | |
2896 | } | |
2897 | ||
771fb4d8 | 2898 | /** |
75c70128 | 2899 | * mpol_misplaced - check whether current folio node is valid in policy |
771fb4d8 | 2900 | * |
75c70128 | 2901 | * @folio: folio to be checked |
f8fd525b | 2902 | * @vmf: structure describing the fault |
75c70128 | 2903 | * @addr: virtual address in @vma for shared policy lookup and interleave policy |
771fb4d8 | 2904 | * |
75c70128 | 2905 | * Lookup current policy node id for vma,addr and "compare to" folio's |
5f076944 | 2906 | * node id. Policy determination "mimics" alloc_page_vma(). |
771fb4d8 | 2907 | * Called from fault path where we know the vma and faulting address. |
5f076944 | 2908 | * |
062db293 | 2909 | * Return: NUMA_NO_NODE if the page is in a node that is valid for this |
75c70128 | 2910 | * policy, or a suitable node ID to allocate a replacement folio from. |
771fb4d8 | 2911 | */ |
f8fd525b | 2912 | int mpol_misplaced(struct folio *folio, struct vm_fault *vmf, |
75c70128 | 2913 | unsigned long addr) |
771fb4d8 LS |
2914 | { |
2915 | struct mempolicy *pol; | |
ddc1a5cb | 2916 | pgoff_t ilx; |
c33d6c06 | 2917 | struct zoneref *z; |
75c70128 | 2918 | int curnid = folio_nid(folio); |
f8fd525b | 2919 | struct vm_area_struct *vma = vmf->vma; |
90572890 | 2920 | int thiscpu = raw_smp_processor_id(); |
f8fd525b | 2921 | int thisnid = numa_node_id(); |
98fa15f3 | 2922 | int polnid = NUMA_NO_NODE; |
062db293 | 2923 | int ret = NUMA_NO_NODE; |
771fb4d8 | 2924 | |
f8fd525b DT |
2925 | /* |
2926 | * Make sure ptl is held so that we don't preempt and we | |
2927 | * have a stable smp processor id | |
2928 | */ | |
2929 | lockdep_assert_held(vmf->ptl); | |
ddc1a5cb | 2930 | pol = get_vma_policy(vma, addr, folio_order(folio), &ilx); |
771fb4d8 LS |
2931 | if (!(pol->flags & MPOL_F_MOF)) |
2932 | goto out; | |
2933 | ||
2934 | switch (pol->mode) { | |
2935 | case MPOL_INTERLEAVE: | |
ddc1a5cb | 2936 | polnid = interleave_nid(pol, ilx); |
771fb4d8 LS |
2937 | break; |
2938 | ||
fa3bea4e GP |
2939 | case MPOL_WEIGHTED_INTERLEAVE: |
2940 | polnid = weighted_interleave_nid(pol, ilx); | |
2941 | break; | |
2942 | ||
771fb4d8 | 2943 | case MPOL_PREFERRED: |
b27abacc DH |
2944 | if (node_isset(curnid, pol->nodes)) |
2945 | goto out; | |
269fbe72 | 2946 | polnid = first_node(pol->nodes); |
7858d7bc FT |
2947 | break; |
2948 | ||
2949 | case MPOL_LOCAL: | |
2950 | polnid = numa_node_id(); | |
771fb4d8 LS |
2951 | break; |
2952 | ||
2953 | case MPOL_BIND: | |
133d04b1 DT |
2954 | case MPOL_PREFERRED_MANY: |
2955 | /* | |
2956 | * Even though MPOL_PREFERRED_MANY can allocate pages outside | |
2957 | * policy nodemask we don't allow numa migration to nodes | |
2958 | * outside policy nodemask for now. This is done so that if we | |
2959 | * want demotion to slow memory to happen, before allocating | |
2960 | * from some DRAM node say 'x', we will end up using a | |
2961 | * MPOL_PREFERRED_MANY mask excluding node 'x'. In such scenario | |
2962 | * we should not promote to node 'x' from slow memory node. | |
2963 | */ | |
bda420b9 | 2964 | if (pol->flags & MPOL_F_MORON) { |
133d04b1 DT |
2965 | /* |
2966 | * Optimize placement among multiple nodes | |
2967 | * via NUMA balancing | |
2968 | */ | |
269fbe72 | 2969 | if (node_isset(thisnid, pol->nodes)) |
bda420b9 HY |
2970 | break; |
2971 | goto out; | |
2972 | } | |
c33d6c06 | 2973 | |
771fb4d8 | 2974 | /* |
771fb4d8 LS |
2975 | * use current page if in policy nodemask, |
2976 | * else select nearest allowed node, if any. | |
2977 | * If no allowed nodes, use current [!misplaced]. | |
2978 | */ | |
269fbe72 | 2979 | if (node_isset(curnid, pol->nodes)) |
771fb4d8 | 2980 | goto out; |
c33d6c06 | 2981 | z = first_zones_zonelist( |
f8fd525b | 2982 | node_zonelist(thisnid, GFP_HIGHUSER), |
771fb4d8 | 2983 | gfp_zone(GFP_HIGHUSER), |
269fbe72 | 2984 | &pol->nodes); |
29943248 | 2985 | polnid = zonelist_node_idx(z); |
771fb4d8 LS |
2986 | break; |
2987 | ||
2988 | default: | |
2989 | BUG(); | |
2990 | } | |
5606e387 | 2991 | |
75c70128 | 2992 | /* Migrate the folio towards the node whose CPU is referencing it */ |
e42c8ff2 | 2993 | if (pol->flags & MPOL_F_MORON) { |
90572890 | 2994 | polnid = thisnid; |
5606e387 | 2995 | |
8c9ae56d | 2996 | if (!should_numa_migrate_memory(current, folio, curnid, |
75c70128 | 2997 | thiscpu)) |
de1c9ce6 | 2998 | goto out; |
e42c8ff2 MG |
2999 | } |
3000 | ||
771fb4d8 LS |
3001 | if (curnid != polnid) |
3002 | ret = polnid; | |
3003 | out: | |
3004 | mpol_cond_put(pol); | |
3005 | ||
3006 | return ret; | |
3007 | } | |
3008 | ||
c11600e4 DR |
3009 | /* |
3010 | * Drop the (possibly final) reference to task->mempolicy. It needs to be | |
3011 | * dropped after task->mempolicy is set to NULL so that any allocation done as | |
3012 | * part of its kmem_cache_free(), such as by KASAN, doesn't reference a freed | |
3013 | * policy. | |
3014 | */ | |
3015 | void mpol_put_task_policy(struct task_struct *task) | |
3016 | { | |
3017 | struct mempolicy *pol; | |
3018 | ||
3019 | task_lock(task); | |
3020 | pol = task->mempolicy; | |
3021 | task->mempolicy = NULL; | |
3022 | task_unlock(task); | |
3023 | mpol_put(pol); | |
3024 | } | |
3025 | ||
1da177e4 LT |
3026 | static void sp_delete(struct shared_policy *sp, struct sp_node *n) |
3027 | { | |
1da177e4 | 3028 | rb_erase(&n->nd, &sp->root); |
63f74ca2 | 3029 | sp_free(n); |
1da177e4 LT |
3030 | } |
3031 | ||
42288fe3 MG |
3032 | static void sp_node_init(struct sp_node *node, unsigned long start, |
3033 | unsigned long end, struct mempolicy *pol) | |
3034 | { | |
3035 | node->start = start; | |
3036 | node->end = end; | |
3037 | node->policy = pol; | |
3038 | } | |
3039 | ||
dbcb0f19 AB |
3040 | static struct sp_node *sp_alloc(unsigned long start, unsigned long end, |
3041 | struct mempolicy *pol) | |
1da177e4 | 3042 | { |
869833f2 KM |
3043 | struct sp_node *n; |
3044 | struct mempolicy *newpol; | |
1da177e4 | 3045 | |
869833f2 | 3046 | n = kmem_cache_alloc(sn_cache, GFP_KERNEL); |
1da177e4 LT |
3047 | if (!n) |
3048 | return NULL; | |
869833f2 KM |
3049 | |
3050 | newpol = mpol_dup(pol); | |
3051 | if (IS_ERR(newpol)) { | |
3052 | kmem_cache_free(sn_cache, n); | |
3053 | return NULL; | |
3054 | } | |
3055 | newpol->flags |= MPOL_F_SHARED; | |
42288fe3 | 3056 | sp_node_init(n, start, end, newpol); |
869833f2 | 3057 | |
1da177e4 LT |
3058 | return n; |
3059 | } | |
3060 | ||
3061 | /* Replace a policy range. */ | |
93397c3b HD |
3062 | static int shared_policy_replace(struct shared_policy *sp, pgoff_t start, |
3063 | pgoff_t end, struct sp_node *new) | |
1da177e4 | 3064 | { |
b22d127a | 3065 | struct sp_node *n; |
42288fe3 MG |
3066 | struct sp_node *n_new = NULL; |
3067 | struct mempolicy *mpol_new = NULL; | |
b22d127a | 3068 | int ret = 0; |
1da177e4 | 3069 | |
42288fe3 | 3070 | restart: |
4a8c7bb5 | 3071 | write_lock(&sp->lock); |
1da177e4 LT |
3072 | n = sp_lookup(sp, start, end); |
3073 | /* Take care of old policies in the same range. */ | |
3074 | while (n && n->start < end) { | |
3075 | struct rb_node *next = rb_next(&n->nd); | |
3076 | if (n->start >= start) { | |
3077 | if (n->end <= end) | |
3078 | sp_delete(sp, n); | |
3079 | else | |
3080 | n->start = end; | |
3081 | } else { | |
3082 | /* Old policy spanning whole new range. */ | |
3083 | if (n->end > end) { | |
42288fe3 MG |
3084 | if (!n_new) |
3085 | goto alloc_new; | |
3086 | ||
3087 | *mpol_new = *n->policy; | |
3088 | atomic_set(&mpol_new->refcnt, 1); | |
7880639c | 3089 | sp_node_init(n_new, end, n->end, mpol_new); |
1da177e4 | 3090 | n->end = start; |
5ca39575 | 3091 | sp_insert(sp, n_new); |
42288fe3 MG |
3092 | n_new = NULL; |
3093 | mpol_new = NULL; | |
1da177e4 LT |
3094 | break; |
3095 | } else | |
3096 | n->end = start; | |
3097 | } | |
3098 | if (!next) | |
3099 | break; | |
3100 | n = rb_entry(next, struct sp_node, nd); | |
3101 | } | |
3102 | if (new) | |
3103 | sp_insert(sp, new); | |
4a8c7bb5 | 3104 | write_unlock(&sp->lock); |
42288fe3 MG |
3105 | ret = 0; |
3106 | ||
3107 | err_out: | |
3108 | if (mpol_new) | |
3109 | mpol_put(mpol_new); | |
3110 | if (n_new) | |
3111 | kmem_cache_free(sn_cache, n_new); | |
3112 | ||
b22d127a | 3113 | return ret; |
42288fe3 MG |
3114 | |
3115 | alloc_new: | |
4a8c7bb5 | 3116 | write_unlock(&sp->lock); |
42288fe3 MG |
3117 | ret = -ENOMEM; |
3118 | n_new = kmem_cache_alloc(sn_cache, GFP_KERNEL); | |
3119 | if (!n_new) | |
3120 | goto err_out; | |
3121 | mpol_new = kmem_cache_alloc(policy_cache, GFP_KERNEL); | |
3122 | if (!mpol_new) | |
3123 | goto err_out; | |
4ad09955 | 3124 | atomic_set(&mpol_new->refcnt, 1); |
42288fe3 | 3125 | goto restart; |
1da177e4 LT |
3126 | } |
3127 | ||
71fe804b LS |
3128 | /** |
3129 | * mpol_shared_policy_init - initialize shared policy for inode | |
3130 | * @sp: pointer to inode shared policy | |
3131 | * @mpol: struct mempolicy to install | |
3132 | * | |
3133 | * Install non-NULL @mpol in inode's shared policy rb-tree. | |
3134 | * On entry, the current task has a reference on a non-NULL @mpol. | |
3135 | * This must be released on exit. | |
4bfc4495 | 3136 | * This is called at get_inode() calls and we can use GFP_KERNEL. |
71fe804b LS |
3137 | */ |
3138 | void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol) | |
3139 | { | |
58568d2a MX |
3140 | int ret; |
3141 | ||
71fe804b | 3142 | sp->root = RB_ROOT; /* empty tree == default mempolicy */ |
4a8c7bb5 | 3143 | rwlock_init(&sp->lock); |
71fe804b LS |
3144 | |
3145 | if (mpol) { | |
35ec8fa0 HD |
3146 | struct sp_node *sn; |
3147 | struct mempolicy *npol; | |
4bfc4495 | 3148 | NODEMASK_SCRATCH(scratch); |
71fe804b | 3149 | |
4bfc4495 | 3150 | if (!scratch) |
5c0c1654 | 3151 | goto put_mpol; |
35ec8fa0 HD |
3152 | |
3153 | /* contextualize the tmpfs mount point mempolicy to this file */ | |
3154 | npol = mpol_new(mpol->mode, mpol->flags, &mpol->w.user_nodemask); | |
3155 | if (IS_ERR(npol)) | |
0cae3457 | 3156 | goto free_scratch; /* no valid nodemask intersection */ |
58568d2a MX |
3157 | |
3158 | task_lock(current); | |
35ec8fa0 | 3159 | ret = mpol_set_nodemask(npol, &mpol->w.user_nodemask, scratch); |
58568d2a | 3160 | task_unlock(current); |
15d77835 | 3161 | if (ret) |
35ec8fa0 HD |
3162 | goto put_npol; |
3163 | ||
3164 | /* alloc node covering entire file; adds ref to file's npol */ | |
3165 | sn = sp_alloc(0, MAX_LFS_FILESIZE >> PAGE_SHIFT, npol); | |
3166 | if (sn) | |
3167 | sp_insert(sp, sn); | |
3168 | put_npol: | |
3169 | mpol_put(npol); /* drop initial ref on file's npol */ | |
0cae3457 | 3170 | free_scratch: |
4bfc4495 | 3171 | NODEMASK_SCRATCH_FREE(scratch); |
5c0c1654 LS |
3172 | put_mpol: |
3173 | mpol_put(mpol); /* drop our incoming ref on sb mpol */ | |
7339ff83 RH |
3174 | } |
3175 | } | |
3176 | ||
c36f6e6d HD |
3177 | int mpol_set_shared_policy(struct shared_policy *sp, |
3178 | struct vm_area_struct *vma, struct mempolicy *pol) | |
1da177e4 LT |
3179 | { |
3180 | int err; | |
3181 | struct sp_node *new = NULL; | |
3182 | unsigned long sz = vma_pages(vma); | |
3183 | ||
c36f6e6d HD |
3184 | if (pol) { |
3185 | new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, pol); | |
1da177e4 LT |
3186 | if (!new) |
3187 | return -ENOMEM; | |
3188 | } | |
c36f6e6d | 3189 | err = shared_policy_replace(sp, vma->vm_pgoff, vma->vm_pgoff + sz, new); |
1da177e4 | 3190 | if (err && new) |
63f74ca2 | 3191 | sp_free(new); |
1da177e4 LT |
3192 | return err; |
3193 | } | |
3194 | ||
3195 | /* Free a backing policy store on inode delete. */ | |
c36f6e6d | 3196 | void mpol_free_shared_policy(struct shared_policy *sp) |
1da177e4 LT |
3197 | { |
3198 | struct sp_node *n; | |
3199 | struct rb_node *next; | |
3200 | ||
c36f6e6d | 3201 | if (!sp->root.rb_node) |
1da177e4 | 3202 | return; |
c36f6e6d HD |
3203 | write_lock(&sp->lock); |
3204 | next = rb_first(&sp->root); | |
1da177e4 LT |
3205 | while (next) { |
3206 | n = rb_entry(next, struct sp_node, nd); | |
3207 | next = rb_next(&n->nd); | |
c36f6e6d | 3208 | sp_delete(sp, n); |
1da177e4 | 3209 | } |
c36f6e6d | 3210 | write_unlock(&sp->lock); |
1da177e4 LT |
3211 | } |
3212 | ||
1a687c2e | 3213 | #ifdef CONFIG_NUMA_BALANCING |
c297663c | 3214 | static int __initdata numabalancing_override; |
1a687c2e MG |
3215 | |
3216 | static void __init check_numabalancing_enable(void) | |
3217 | { | |
3218 | bool numabalancing_default = false; | |
3219 | ||
3220 | if (IS_ENABLED(CONFIG_NUMA_BALANCING_DEFAULT_ENABLED)) | |
3221 | numabalancing_default = true; | |
3222 | ||
c297663c MG |
3223 | /* Parsed by setup_numabalancing. override == 1 enables, -1 disables */ |
3224 | if (numabalancing_override) | |
3225 | set_numabalancing_state(numabalancing_override == 1); | |
3226 | ||
b0dc2b9b | 3227 | if (num_online_nodes() > 1 && !numabalancing_override) { |
756a025f | 3228 | pr_info("%s automatic NUMA balancing. Configure with numa_balancing= or the kernel.numa_balancing sysctl\n", |
c297663c | 3229 | numabalancing_default ? "Enabling" : "Disabling"); |
1a687c2e MG |
3230 | set_numabalancing_state(numabalancing_default); |
3231 | } | |
3232 | } | |
3233 | ||
3234 | static int __init setup_numabalancing(char *str) | |
3235 | { | |
3236 | int ret = 0; | |
3237 | if (!str) | |
3238 | goto out; | |
1a687c2e MG |
3239 | |
3240 | if (!strcmp(str, "enable")) { | |
c297663c | 3241 | numabalancing_override = 1; |
1a687c2e MG |
3242 | ret = 1; |
3243 | } else if (!strcmp(str, "disable")) { | |
c297663c | 3244 | numabalancing_override = -1; |
1a687c2e MG |
3245 | ret = 1; |
3246 | } | |
3247 | out: | |
3248 | if (!ret) | |
4a404bea | 3249 | pr_warn("Unable to parse numa_balancing=\n"); |
1a687c2e MG |
3250 | |
3251 | return ret; | |
3252 | } | |
3253 | __setup("numa_balancing=", setup_numabalancing); | |
3254 | #else | |
3255 | static inline void __init check_numabalancing_enable(void) | |
3256 | { | |
3257 | } | |
3258 | #endif /* CONFIG_NUMA_BALANCING */ | |
3259 | ||
1da177e4 LT |
3260 | void __init numa_policy_init(void) |
3261 | { | |
b71636e2 PM |
3262 | nodemask_t interleave_nodes; |
3263 | unsigned long largest = 0; | |
3264 | int nid, prefer = 0; | |
3265 | ||
1da177e4 LT |
3266 | policy_cache = kmem_cache_create("numa_policy", |
3267 | sizeof(struct mempolicy), | |
20c2df83 | 3268 | 0, SLAB_PANIC, NULL); |
1da177e4 LT |
3269 | |
3270 | sn_cache = kmem_cache_create("shared_policy_node", | |
3271 | sizeof(struct sp_node), | |
20c2df83 | 3272 | 0, SLAB_PANIC, NULL); |
1da177e4 | 3273 | |
5606e387 MG |
3274 | for_each_node(nid) { |
3275 | preferred_node_policy[nid] = (struct mempolicy) { | |
3276 | .refcnt = ATOMIC_INIT(1), | |
3277 | .mode = MPOL_PREFERRED, | |
3278 | .flags = MPOL_F_MOF | MPOL_F_MORON, | |
269fbe72 | 3279 | .nodes = nodemask_of_node(nid), |
5606e387 MG |
3280 | }; |
3281 | } | |
3282 | ||
b71636e2 PM |
3283 | /* |
3284 | * Set interleaving policy for system init. Interleaving is only | |
3285 | * enabled across suitably sized nodes (default is >= 16MB), or | |
3286 | * fall back to the largest node if they're all smaller. | |
3287 | */ | |
3288 | nodes_clear(interleave_nodes); | |
01f13bd6 | 3289 | for_each_node_state(nid, N_MEMORY) { |
b71636e2 PM |
3290 | unsigned long total_pages = node_present_pages(nid); |
3291 | ||
3292 | /* Preserve the largest node */ | |
3293 | if (largest < total_pages) { | |
3294 | largest = total_pages; | |
3295 | prefer = nid; | |
3296 | } | |
3297 | ||
3298 | /* Interleave this node? */ | |
3299 | if ((total_pages << PAGE_SHIFT) >= (16 << 20)) | |
3300 | node_set(nid, interleave_nodes); | |
3301 | } | |
3302 | ||
3303 | /* All too small, use the largest */ | |
3304 | if (unlikely(nodes_empty(interleave_nodes))) | |
3305 | node_set(prefer, interleave_nodes); | |
1da177e4 | 3306 | |
028fec41 | 3307 | if (do_set_mempolicy(MPOL_INTERLEAVE, 0, &interleave_nodes)) |
b1de0d13 | 3308 | pr_err("%s: interleaving failed\n", __func__); |
1a687c2e MG |
3309 | |
3310 | check_numabalancing_enable(); | |
1da177e4 LT |
3311 | } |
3312 | ||
8bccd85f | 3313 | /* Reset policy of current process to default */ |
1da177e4 LT |
3314 | void numa_default_policy(void) |
3315 | { | |
028fec41 | 3316 | do_set_mempolicy(MPOL_DEFAULT, 0, NULL); |
1da177e4 | 3317 | } |
68860ec1 | 3318 | |
095f1fc4 LS |
3319 | /* |
3320 | * Parse and format mempolicy from/to strings | |
3321 | */ | |
345ace9c LS |
3322 | static const char * const policy_modes[] = |
3323 | { | |
3324 | [MPOL_DEFAULT] = "default", | |
3325 | [MPOL_PREFERRED] = "prefer", | |
3326 | [MPOL_BIND] = "bind", | |
3327 | [MPOL_INTERLEAVE] = "interleave", | |
fa3bea4e | 3328 | [MPOL_WEIGHTED_INTERLEAVE] = "weighted interleave", |
d3a71033 | 3329 | [MPOL_LOCAL] = "local", |
b27abacc | 3330 | [MPOL_PREFERRED_MANY] = "prefer (many)", |
345ace9c | 3331 | }; |
1a75a6c8 | 3332 | |
095f1fc4 LS |
3333 | #ifdef CONFIG_TMPFS |
3334 | /** | |
f2a07f40 | 3335 | * mpol_parse_str - parse string to mempolicy, for tmpfs mpol mount option. |
095f1fc4 | 3336 | * @str: string containing mempolicy to parse |
71fe804b | 3337 | * @mpol: pointer to struct mempolicy pointer, returned on success. |
095f1fc4 LS |
3338 | * |
3339 | * Format of input: | |
3340 | * <mode>[=<flags>][:<nodelist>] | |
3341 | * | |
dad5b023 | 3342 | * Return: %0 on success, else %1 |
095f1fc4 | 3343 | */ |
a7a88b23 | 3344 | int mpol_parse_str(char *str, struct mempolicy **mpol) |
095f1fc4 | 3345 | { |
71fe804b | 3346 | struct mempolicy *new = NULL; |
f2a07f40 | 3347 | unsigned short mode_flags; |
71fe804b | 3348 | nodemask_t nodes; |
095f1fc4 LS |
3349 | char *nodelist = strchr(str, ':'); |
3350 | char *flags = strchr(str, '='); | |
dedf2c73 | 3351 | int err = 1, mode; |
095f1fc4 | 3352 | |
c7a91bc7 DC |
3353 | if (flags) |
3354 | *flags++ = '\0'; /* terminate mode string */ | |
3355 | ||
095f1fc4 LS |
3356 | if (nodelist) { |
3357 | /* NUL-terminate mode or flags string */ | |
3358 | *nodelist++ = '\0'; | |
71fe804b | 3359 | if (nodelist_parse(nodelist, nodes)) |
095f1fc4 | 3360 | goto out; |
01f13bd6 | 3361 | if (!nodes_subset(nodes, node_states[N_MEMORY])) |
095f1fc4 | 3362 | goto out; |
71fe804b LS |
3363 | } else |
3364 | nodes_clear(nodes); | |
3365 | ||
dedf2c73 | 3366 | mode = match_string(policy_modes, MPOL_MAX, str); |
3367 | if (mode < 0) | |
095f1fc4 LS |
3368 | goto out; |
3369 | ||
71fe804b | 3370 | switch (mode) { |
095f1fc4 | 3371 | case MPOL_PREFERRED: |
71fe804b | 3372 | /* |
aa9f7d51 RD |
3373 | * Insist on a nodelist of one node only, although later |
3374 | * we use first_node(nodes) to grab a single node, so here | |
3375 | * nodelist (or nodes) cannot be empty. | |
71fe804b | 3376 | */ |
095f1fc4 LS |
3377 | if (nodelist) { |
3378 | char *rest = nodelist; | |
3379 | while (isdigit(*rest)) | |
3380 | rest++; | |
926f2ae0 KM |
3381 | if (*rest) |
3382 | goto out; | |
aa9f7d51 RD |
3383 | if (nodes_empty(nodes)) |
3384 | goto out; | |
095f1fc4 LS |
3385 | } |
3386 | break; | |
095f1fc4 | 3387 | case MPOL_INTERLEAVE: |
fa3bea4e | 3388 | case MPOL_WEIGHTED_INTERLEAVE: |
095f1fc4 LS |
3389 | /* |
3390 | * Default to online nodes with memory if no nodelist | |
3391 | */ | |
3392 | if (!nodelist) | |
01f13bd6 | 3393 | nodes = node_states[N_MEMORY]; |
3f226aa1 | 3394 | break; |
71fe804b | 3395 | case MPOL_LOCAL: |
3f226aa1 | 3396 | /* |
71fe804b | 3397 | * Don't allow a nodelist; mpol_new() checks flags |
3f226aa1 | 3398 | */ |
71fe804b | 3399 | if (nodelist) |
3f226aa1 | 3400 | goto out; |
3f226aa1 | 3401 | break; |
413b43de RT |
3402 | case MPOL_DEFAULT: |
3403 | /* | |
3404 | * Insist on a empty nodelist | |
3405 | */ | |
3406 | if (!nodelist) | |
3407 | err = 0; | |
3408 | goto out; | |
b27abacc | 3409 | case MPOL_PREFERRED_MANY: |
d69b2e63 KM |
3410 | case MPOL_BIND: |
3411 | /* | |
3412 | * Insist on a nodelist | |
3413 | */ | |
3414 | if (!nodelist) | |
3415 | goto out; | |
095f1fc4 LS |
3416 | } |
3417 | ||
71fe804b | 3418 | mode_flags = 0; |
095f1fc4 LS |
3419 | if (flags) { |
3420 | /* | |
3421 | * Currently, we only support two mutually exclusive | |
3422 | * mode flags. | |
3423 | */ | |
3424 | if (!strcmp(flags, "static")) | |
71fe804b | 3425 | mode_flags |= MPOL_F_STATIC_NODES; |
095f1fc4 | 3426 | else if (!strcmp(flags, "relative")) |
71fe804b | 3427 | mode_flags |= MPOL_F_RELATIVE_NODES; |
095f1fc4 | 3428 | else |
926f2ae0 | 3429 | goto out; |
095f1fc4 | 3430 | } |
71fe804b LS |
3431 | |
3432 | new = mpol_new(mode, mode_flags, &nodes); | |
3433 | if (IS_ERR(new)) | |
926f2ae0 KM |
3434 | goto out; |
3435 | ||
f2a07f40 HD |
3436 | /* |
3437 | * Save nodes for mpol_to_str() to show the tmpfs mount options | |
3438 | * for /proc/mounts, /proc/pid/mounts and /proc/pid/mountinfo. | |
3439 | */ | |
269fbe72 BW |
3440 | if (mode != MPOL_PREFERRED) { |
3441 | new->nodes = nodes; | |
3442 | } else if (nodelist) { | |
3443 | nodes_clear(new->nodes); | |
3444 | node_set(first_node(nodes), new->nodes); | |
3445 | } else { | |
7858d7bc | 3446 | new->mode = MPOL_LOCAL; |
269fbe72 | 3447 | } |
f2a07f40 HD |
3448 | |
3449 | /* | |
3450 | * Save nodes for contextualization: this will be used to "clone" | |
3451 | * the mempolicy in a specific context [cpuset] at a later time. | |
3452 | */ | |
3453 | new->w.user_nodemask = nodes; | |
3454 | ||
926f2ae0 | 3455 | err = 0; |
71fe804b | 3456 | |
095f1fc4 LS |
3457 | out: |
3458 | /* Restore string for error message */ | |
3459 | if (nodelist) | |
3460 | *--nodelist = ':'; | |
3461 | if (flags) | |
3462 | *--flags = '='; | |
71fe804b LS |
3463 | if (!err) |
3464 | *mpol = new; | |
095f1fc4 LS |
3465 | return err; |
3466 | } | |
3467 | #endif /* CONFIG_TMPFS */ | |
3468 | ||
71fe804b LS |
3469 | /** |
3470 | * mpol_to_str - format a mempolicy structure for printing | |
3471 | * @buffer: to contain formatted mempolicy string | |
3472 | * @maxlen: length of @buffer | |
3473 | * @pol: pointer to mempolicy to be formatted | |
71fe804b | 3474 | * |
948927ee | 3475 | * Convert @pol into a string. If @buffer is too short, truncate the string. |
af649773 TU |
3476 | * Recommend a @maxlen of at least 51 for the longest mode, "weighted |
3477 | * interleave", plus the longest flag flags, "relative|balancing", and to | |
3478 | * display at least a few node ids. | |
1a75a6c8 | 3479 | */ |
948927ee | 3480 | void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) |
1a75a6c8 CL |
3481 | { |
3482 | char *p = buffer; | |
948927ee DR |
3483 | nodemask_t nodes = NODE_MASK_NONE; |
3484 | unsigned short mode = MPOL_DEFAULT; | |
3485 | unsigned short flags = 0; | |
2291990a | 3486 | |
af649773 TU |
3487 | if (pol && |
3488 | pol != &default_policy && | |
3489 | !(pol >= &preferred_node_policy[0] && | |
3490 | pol <= &preferred_node_policy[ARRAY_SIZE(preferred_node_policy) - 1])) { | |
bea904d5 | 3491 | mode = pol->mode; |
948927ee DR |
3492 | flags = pol->flags; |
3493 | } | |
bea904d5 | 3494 | |
1a75a6c8 CL |
3495 | switch (mode) { |
3496 | case MPOL_DEFAULT: | |
7858d7bc | 3497 | case MPOL_LOCAL: |
1a75a6c8 | 3498 | break; |
1a75a6c8 | 3499 | case MPOL_PREFERRED: |
b27abacc | 3500 | case MPOL_PREFERRED_MANY: |
1a75a6c8 | 3501 | case MPOL_BIND: |
1a75a6c8 | 3502 | case MPOL_INTERLEAVE: |
fa3bea4e | 3503 | case MPOL_WEIGHTED_INTERLEAVE: |
269fbe72 | 3504 | nodes = pol->nodes; |
1a75a6c8 | 3505 | break; |
1a75a6c8 | 3506 | default: |
948927ee DR |
3507 | WARN_ON_ONCE(1); |
3508 | snprintf(p, maxlen, "unknown"); | |
3509 | return; | |
1a75a6c8 CL |
3510 | } |
3511 | ||
b7a9f420 | 3512 | p += snprintf(p, maxlen, "%s", policy_modes[mode]); |
1a75a6c8 | 3513 | |
fc36b8d3 | 3514 | if (flags & MPOL_MODE_FLAGS) { |
948927ee | 3515 | p += snprintf(p, buffer + maxlen - p, "="); |
f5b087b5 | 3516 | |
2291990a | 3517 | /* |
af649773 | 3518 | * Static and relative are mutually exclusive. |
2291990a | 3519 | */ |
f5b087b5 | 3520 | if (flags & MPOL_F_STATIC_NODES) |
2291990a LS |
3521 | p += snprintf(p, buffer + maxlen - p, "static"); |
3522 | else if (flags & MPOL_F_RELATIVE_NODES) | |
3523 | p += snprintf(p, buffer + maxlen - p, "relative"); | |
af649773 TU |
3524 | |
3525 | if (flags & MPOL_F_NUMA_BALANCING) { | |
3526 | if (!is_power_of_2(flags & MPOL_MODE_FLAGS)) | |
3527 | p += snprintf(p, buffer + maxlen - p, "|"); | |
3528 | p += snprintf(p, buffer + maxlen - p, "balancing"); | |
3529 | } | |
f5b087b5 DR |
3530 | } |
3531 | ||
9e763e0f TH |
3532 | if (!nodes_empty(nodes)) |
3533 | p += scnprintf(p, buffer + maxlen - p, ":%*pbl", | |
3534 | nodemask_pr_args(&nodes)); | |
1a75a6c8 | 3535 | } |
dce41f5a RK |
3536 | |
3537 | #ifdef CONFIG_SYSFS | |
3538 | struct iw_node_attr { | |
3539 | struct kobj_attribute kobj_attr; | |
3540 | int nid; | |
3541 | }; | |
3542 | ||
cf8cecf2 RK |
3543 | struct sysfs_wi_group { |
3544 | struct kobject wi_kobj; | |
dec92bf9 | 3545 | struct mutex kobj_lock; |
cf8cecf2 RK |
3546 | struct iw_node_attr *nattrs[]; |
3547 | }; | |
3548 | ||
3549 | static struct sysfs_wi_group *wi_group; | |
3550 | ||
dce41f5a RK |
3551 | static ssize_t node_show(struct kobject *kobj, struct kobj_attribute *attr, |
3552 | char *buf) | |
3553 | { | |
3554 | struct iw_node_attr *node_attr; | |
3555 | u8 weight; | |
3556 | ||
3557 | node_attr = container_of(attr, struct iw_node_attr, kobj_attr); | |
3558 | weight = get_il_weight(node_attr->nid); | |
3559 | return sysfs_emit(buf, "%d\n", weight); | |
3560 | } | |
3561 | ||
3562 | static ssize_t node_store(struct kobject *kobj, struct kobj_attribute *attr, | |
3563 | const char *buf, size_t count) | |
3564 | { | |
e341f9c3 | 3565 | struct weighted_interleave_state *new_wi_state, *old_wi_state = NULL; |
dce41f5a | 3566 | struct iw_node_attr *node_attr; |
dce41f5a | 3567 | u8 weight = 0; |
e341f9c3 | 3568 | int i; |
dce41f5a RK |
3569 | |
3570 | node_attr = container_of(attr, struct iw_node_attr, kobj_attr); | |
e341f9c3 JH |
3571 | if (count == 0 || sysfs_streq(buf, "") || |
3572 | kstrtou8(buf, 0, &weight) || weight == 0) | |
dce41f5a RK |
3573 | return -EINVAL; |
3574 | ||
e341f9c3 JH |
3575 | new_wi_state = kzalloc(struct_size(new_wi_state, iw_table, nr_node_ids), |
3576 | GFP_KERNEL); | |
3577 | if (!new_wi_state) | |
dce41f5a RK |
3578 | return -ENOMEM; |
3579 | ||
e341f9c3 JH |
3580 | mutex_lock(&wi_state_lock); |
3581 | old_wi_state = rcu_dereference_protected(wi_state, | |
3582 | lockdep_is_held(&wi_state_lock)); | |
3583 | if (old_wi_state) { | |
3584 | memcpy(new_wi_state->iw_table, old_wi_state->iw_table, | |
3585 | nr_node_ids * sizeof(u8)); | |
3586 | } else { | |
3587 | for (i = 0; i < nr_node_ids; i++) | |
3588 | new_wi_state->iw_table[i] = 1; | |
3589 | } | |
3590 | new_wi_state->iw_table[node_attr->nid] = weight; | |
3591 | new_wi_state->mode_auto = false; | |
3592 | ||
3593 | rcu_assign_pointer(wi_state, new_wi_state); | |
3594 | mutex_unlock(&wi_state_lock); | |
3595 | if (old_wi_state) { | |
3596 | synchronize_rcu(); | |
3597 | kfree(old_wi_state); | |
3598 | } | |
3599 | return count; | |
3600 | } | |
3601 | ||
3602 | static ssize_t weighted_interleave_auto_show(struct kobject *kobj, | |
3603 | struct kobj_attribute *attr, char *buf) | |
3604 | { | |
3605 | struct weighted_interleave_state *state; | |
3606 | bool wi_auto = true; | |
3607 | ||
3608 | rcu_read_lock(); | |
3609 | state = rcu_dereference(wi_state); | |
3610 | if (state) | |
3611 | wi_auto = state->mode_auto; | |
3612 | rcu_read_unlock(); | |
3613 | ||
3614 | return sysfs_emit(buf, "%s\n", str_true_false(wi_auto)); | |
3615 | } | |
3616 | ||
3617 | static ssize_t weighted_interleave_auto_store(struct kobject *kobj, | |
3618 | struct kobj_attribute *attr, const char *buf, size_t count) | |
3619 | { | |
3620 | struct weighted_interleave_state *new_wi_state, *old_wi_state = NULL; | |
3621 | unsigned int *bw; | |
3622 | bool input; | |
3623 | int i; | |
3624 | ||
3625 | if (kstrtobool(buf, &input)) | |
3626 | return -EINVAL; | |
3627 | ||
3628 | new_wi_state = kzalloc(struct_size(new_wi_state, iw_table, nr_node_ids), | |
3629 | GFP_KERNEL); | |
3630 | if (!new_wi_state) | |
3631 | return -ENOMEM; | |
3632 | for (i = 0; i < nr_node_ids; i++) | |
3633 | new_wi_state->iw_table[i] = 1; | |
3634 | ||
3635 | mutex_lock(&wi_state_lock); | |
3636 | if (!input) { | |
3637 | old_wi_state = rcu_dereference_protected(wi_state, | |
3638 | lockdep_is_held(&wi_state_lock)); | |
3639 | if (!old_wi_state) | |
3640 | goto update_wi_state; | |
3641 | if (input == old_wi_state->mode_auto) { | |
3642 | mutex_unlock(&wi_state_lock); | |
3643 | return count; | |
3644 | } | |
3645 | ||
3646 | memcpy(new_wi_state->iw_table, old_wi_state->iw_table, | |
3647 | nr_node_ids * sizeof(u8)); | |
3648 | goto update_wi_state; | |
3649 | } | |
3650 | ||
3651 | bw = node_bw_table; | |
3652 | if (!bw) { | |
3653 | mutex_unlock(&wi_state_lock); | |
3654 | kfree(new_wi_state); | |
3655 | return -ENODEV; | |
3656 | } | |
3657 | ||
3658 | new_wi_state->mode_auto = true; | |
3659 | reduce_interleave_weights(bw, new_wi_state->iw_table); | |
3660 | ||
3661 | update_wi_state: | |
3662 | rcu_assign_pointer(wi_state, new_wi_state); | |
3663 | mutex_unlock(&wi_state_lock); | |
3664 | if (old_wi_state) { | |
3665 | synchronize_rcu(); | |
3666 | kfree(old_wi_state); | |
3667 | } | |
dce41f5a RK |
3668 | return count; |
3669 | } | |
3670 | ||
cf8cecf2 | 3671 | static void sysfs_wi_node_delete(int nid) |
dce41f5a | 3672 | { |
dec92bf9 RK |
3673 | struct iw_node_attr *attr; |
3674 | ||
3675 | if (nid < 0 || nid >= nr_node_ids) | |
3676 | return; | |
3677 | ||
3678 | mutex_lock(&wi_group->kobj_lock); | |
3679 | attr = wi_group->nattrs[nid]; | |
3680 | if (!attr) { | |
3681 | mutex_unlock(&wi_group->kobj_lock); | |
dce41f5a | 3682 | return; |
dec92bf9 RK |
3683 | } |
3684 | ||
3685 | wi_group->nattrs[nid] = NULL; | |
3686 | mutex_unlock(&wi_group->kobj_lock); | |
cf8cecf2 | 3687 | |
dec92bf9 RK |
3688 | sysfs_remove_file(&wi_group->wi_kobj, &attr->kobj_attr.attr); |
3689 | kfree(attr->kobj_attr.attr.name); | |
3690 | kfree(attr); | |
dce41f5a RK |
3691 | } |
3692 | ||
cf8cecf2 | 3693 | static void sysfs_wi_node_delete_all(void) |
dce41f5a | 3694 | { |
bb52e89d | 3695 | int nid; |
dce41f5a | 3696 | |
bb52e89d | 3697 | for (nid = 0; nid < nr_node_ids; nid++) |
cf8cecf2 | 3698 | sysfs_wi_node_delete(nid); |
bb52e89d RK |
3699 | } |
3700 | ||
e341f9c3 | 3701 | static void wi_state_free(void) |
bb52e89d | 3702 | { |
e341f9c3 | 3703 | struct weighted_interleave_state *old_wi_state; |
bb52e89d | 3704 | |
e341f9c3 JH |
3705 | mutex_lock(&wi_state_lock); |
3706 | ||
3707 | old_wi_state = rcu_dereference_protected(wi_state, | |
3708 | lockdep_is_held(&wi_state_lock)); | |
3709 | if (!old_wi_state) { | |
3710 | mutex_unlock(&wi_state_lock); | |
41ffaa0e | 3711 | return; |
e341f9c3 | 3712 | } |
bb52e89d | 3713 | |
e341f9c3 JH |
3714 | rcu_assign_pointer(wi_state, NULL); |
3715 | mutex_unlock(&wi_state_lock); | |
bb52e89d | 3716 | synchronize_rcu(); |
e341f9c3 | 3717 | kfree(old_wi_state); |
bb52e89d RK |
3718 | } |
3719 | ||
e341f9c3 JH |
3720 | static struct kobj_attribute wi_auto_attr = |
3721 | __ATTR(auto, 0664, weighted_interleave_auto_show, | |
3722 | weighted_interleave_auto_store); | |
3723 | ||
cf8cecf2 | 3724 | static void wi_cleanup(void) { |
e341f9c3 | 3725 | sysfs_remove_file(&wi_group->wi_kobj, &wi_auto_attr.attr); |
cf8cecf2 | 3726 | sysfs_wi_node_delete_all(); |
e341f9c3 | 3727 | wi_state_free(); |
bb52e89d RK |
3728 | } |
3729 | ||
3730 | static void wi_kobj_release(struct kobject *wi_kobj) | |
3731 | { | |
cf8cecf2 | 3732 | kfree(wi_group); |
dce41f5a RK |
3733 | } |
3734 | ||
3735 | static const struct kobj_type wi_ktype = { | |
3736 | .sysfs_ops = &kobj_sysfs_ops, | |
bb52e89d | 3737 | .release = wi_kobj_release, |
dce41f5a RK |
3738 | }; |
3739 | ||
cf8cecf2 | 3740 | static int sysfs_wi_node_add(int nid) |
dce41f5a | 3741 | { |
dec92bf9 | 3742 | int ret; |
dce41f5a | 3743 | char *name; |
dec92bf9 | 3744 | struct iw_node_attr *new_attr; |
dce41f5a | 3745 | |
dec92bf9 RK |
3746 | if (nid < 0 || nid >= nr_node_ids) { |
3747 | pr_err("invalid node id: %d\n", nid); | |
3748 | return -EINVAL; | |
3749 | } | |
3750 | ||
3751 | new_attr = kzalloc(sizeof(*new_attr), GFP_KERNEL); | |
3752 | if (!new_attr) | |
dce41f5a RK |
3753 | return -ENOMEM; |
3754 | ||
3755 | name = kasprintf(GFP_KERNEL, "node%d", nid); | |
3756 | if (!name) { | |
dec92bf9 | 3757 | kfree(new_attr); |
dce41f5a RK |
3758 | return -ENOMEM; |
3759 | } | |
3760 | ||
dec92bf9 RK |
3761 | sysfs_attr_init(&new_attr->kobj_attr.attr); |
3762 | new_attr->kobj_attr.attr.name = name; | |
3763 | new_attr->kobj_attr.attr.mode = 0644; | |
3764 | new_attr->kobj_attr.show = node_show; | |
3765 | new_attr->kobj_attr.store = node_store; | |
3766 | new_attr->nid = nid; | |
dce41f5a | 3767 | |
dec92bf9 RK |
3768 | mutex_lock(&wi_group->kobj_lock); |
3769 | if (wi_group->nattrs[nid]) { | |
3770 | mutex_unlock(&wi_group->kobj_lock); | |
3771 | ret = -EEXIST; | |
3772 | goto out; | |
dce41f5a RK |
3773 | } |
3774 | ||
dec92bf9 RK |
3775 | ret = sysfs_create_file(&wi_group->wi_kobj, &new_attr->kobj_attr.attr); |
3776 | if (ret) { | |
3777 | mutex_unlock(&wi_group->kobj_lock); | |
3778 | goto out; | |
3779 | } | |
3780 | wi_group->nattrs[nid] = new_attr; | |
3781 | mutex_unlock(&wi_group->kobj_lock); | |
dce41f5a | 3782 | return 0; |
dec92bf9 RK |
3783 | |
3784 | out: | |
3785 | kfree(new_attr->kobj_attr.attr.name); | |
3786 | kfree(new_attr); | |
3787 | return ret; | |
3788 | } | |
3789 | ||
3790 | static int wi_node_notifier(struct notifier_block *nb, | |
3791 | unsigned long action, void *data) | |
3792 | { | |
3793 | int err; | |
3794 | struct memory_notify *arg = data; | |
3795 | int nid = arg->status_change_nid; | |
3796 | ||
3797 | if (nid < 0) | |
3798 | return NOTIFY_OK; | |
3799 | ||
3800 | switch (action) { | |
3801 | case MEM_ONLINE: | |
3802 | err = sysfs_wi_node_add(nid); | |
3803 | if (err) | |
3804 | pr_err("failed to add sysfs for node%d during hotplug: %d\n", | |
3805 | nid, err); | |
3806 | break; | |
3807 | case MEM_OFFLINE: | |
3808 | sysfs_wi_node_delete(nid); | |
3809 | break; | |
3810 | } | |
3811 | ||
3812 | return NOTIFY_OK; | |
dce41f5a RK |
3813 | } |
3814 | ||
cf8cecf2 | 3815 | static int __init add_weighted_interleave_group(struct kobject *mempolicy_kobj) |
dce41f5a | 3816 | { |
dce41f5a RK |
3817 | int nid, err; |
3818 | ||
cf8cecf2 RK |
3819 | wi_group = kzalloc(struct_size(wi_group, nattrs, nr_node_ids), |
3820 | GFP_KERNEL); | |
3821 | if (!wi_group) | |
dce41f5a | 3822 | return -ENOMEM; |
dec92bf9 | 3823 | mutex_init(&wi_group->kobj_lock); |
dce41f5a | 3824 | |
cf8cecf2 | 3825 | err = kobject_init_and_add(&wi_group->wi_kobj, &wi_ktype, mempolicy_kobj, |
dce41f5a | 3826 | "weighted_interleave"); |
bb52e89d RK |
3827 | if (err) |
3828 | goto err_put_kobj; | |
dce41f5a | 3829 | |
e341f9c3 JH |
3830 | err = sysfs_create_file(&wi_group->wi_kobj, &wi_auto_attr.attr); |
3831 | if (err) | |
3832 | goto err_put_kobj; | |
3833 | ||
dec92bf9 RK |
3834 | for_each_online_node(nid) { |
3835 | if (!node_state(nid, N_MEMORY)) | |
3836 | continue; | |
3837 | ||
cf8cecf2 | 3838 | err = sysfs_wi_node_add(nid); |
dce41f5a | 3839 | if (err) { |
dec92bf9 RK |
3840 | pr_err("failed to add sysfs for node%d during init: %d\n", |
3841 | nid, err); | |
bb52e89d | 3842 | goto err_cleanup_kobj; |
dce41f5a RK |
3843 | } |
3844 | } | |
dce41f5a | 3845 | |
dec92bf9 | 3846 | hotplug_memory_notifier(wi_node_notifier, DEFAULT_CALLBACK_PRI); |
bb52e89d | 3847 | return 0; |
dce41f5a | 3848 | |
bb52e89d | 3849 | err_cleanup_kobj: |
cf8cecf2 RK |
3850 | wi_cleanup(); |
3851 | kobject_del(&wi_group->wi_kobj); | |
bb52e89d | 3852 | err_put_kobj: |
cf8cecf2 | 3853 | kobject_put(&wi_group->wi_kobj); |
bb52e89d | 3854 | return err; |
dce41f5a RK |
3855 | } |
3856 | ||
dce41f5a RK |
3857 | static int __init mempolicy_sysfs_init(void) |
3858 | { | |
3859 | int err; | |
3860 | static struct kobject *mempolicy_kobj; | |
3861 | ||
bb52e89d RK |
3862 | mempolicy_kobj = kobject_create_and_add("mempolicy", mm_kobj); |
3863 | if (!mempolicy_kobj) | |
3864 | return -ENOMEM; | |
dce41f5a | 3865 | |
bb52e89d | 3866 | err = add_weighted_interleave_group(mempolicy_kobj); |
dce41f5a | 3867 | if (err) |
bb52e89d | 3868 | goto err_kobj; |
dce41f5a | 3869 | |
bb52e89d | 3870 | return 0; |
dce41f5a | 3871 | |
bb52e89d RK |
3872 | err_kobj: |
3873 | kobject_del(mempolicy_kobj); | |
3874 | kobject_put(mempolicy_kobj); | |
dce41f5a RK |
3875 | return err; |
3876 | } | |
3877 | ||
3878 | late_initcall(mempolicy_sysfs_init); | |
3879 | #endif /* CONFIG_SYSFS */ |