mempolicy.c 72 KB

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