mempolicy.c 71 KB

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