mempolicy.c 72 KB

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