mempolicy.c 64 KB

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