alloc_nid_api.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include "alloc_nid_api.h"
  3. static int alloc_nid_test_flags = TEST_F_NONE;
  4. /*
  5. * contains the fraction of MEM_SIZE contained in each node in basis point
  6. * units (one hundredth of 1% or 1/10000)
  7. */
  8. static const unsigned int node_fractions[] = {
  9. 2500, /* 1/4 */
  10. 625, /* 1/16 */
  11. 1250, /* 1/8 */
  12. 1250, /* 1/8 */
  13. 625, /* 1/16 */
  14. 625, /* 1/16 */
  15. 2500, /* 1/4 */
  16. 625, /* 1/16 */
  17. };
  18. static inline const char * const get_memblock_alloc_nid_name(int flags)
  19. {
  20. if (flags & TEST_F_EXACT)
  21. return "memblock_alloc_exact_nid_raw";
  22. if (flags & TEST_F_RAW)
  23. return "memblock_alloc_try_nid_raw";
  24. return "memblock_alloc_try_nid";
  25. }
  26. static inline void *run_memblock_alloc_nid(phys_addr_t size,
  27. phys_addr_t align,
  28. phys_addr_t min_addr,
  29. phys_addr_t max_addr, int nid)
  30. {
  31. assert(!(alloc_nid_test_flags & TEST_F_EXACT) ||
  32. (alloc_nid_test_flags & TEST_F_RAW));
  33. /*
  34. * TEST_F_EXACT should be checked before TEST_F_RAW since
  35. * memblock_alloc_exact_nid_raw() performs raw allocations.
  36. */
  37. if (alloc_nid_test_flags & TEST_F_EXACT)
  38. return memblock_alloc_exact_nid_raw(size, align, min_addr,
  39. max_addr, nid);
  40. if (alloc_nid_test_flags & TEST_F_RAW)
  41. return memblock_alloc_try_nid_raw(size, align, min_addr,
  42. max_addr, nid);
  43. return memblock_alloc_try_nid(size, align, min_addr, max_addr, nid);
  44. }
  45. /*
  46. * A simple test that tries to allocate a memory region within min_addr and
  47. * max_addr range:
  48. *
  49. * + +
  50. * | + +-----------+ |
  51. * | | | rgn | |
  52. * +----+-------+-----------+------+
  53. * ^ ^
  54. * | |
  55. * min_addr max_addr
  56. *
  57. * Expect to allocate a region that ends at max_addr.
  58. */
  59. static int alloc_nid_top_down_simple_check(void)
  60. {
  61. struct memblock_region *rgn = &memblock.reserved.regions[0];
  62. void *allocated_ptr = NULL;
  63. phys_addr_t size = SZ_128;
  64. phys_addr_t min_addr;
  65. phys_addr_t max_addr;
  66. phys_addr_t rgn_end;
  67. PREFIX_PUSH();
  68. setup_memblock();
  69. min_addr = memblock_start_of_DRAM() + SMP_CACHE_BYTES * 2;
  70. max_addr = min_addr + SZ_512;
  71. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  72. min_addr, max_addr,
  73. NUMA_NO_NODE);
  74. rgn_end = rgn->base + rgn->size;
  75. ASSERT_NE(allocated_ptr, NULL);
  76. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  77. ASSERT_EQ(rgn->size, size);
  78. ASSERT_EQ(rgn->base, max_addr - size);
  79. ASSERT_EQ(rgn_end, max_addr);
  80. ASSERT_EQ(memblock.reserved.cnt, 1);
  81. ASSERT_EQ(memblock.reserved.total_size, size);
  82. test_pass_pop();
  83. return 0;
  84. }
  85. /*
  86. * A simple test that tries to allocate a memory region within min_addr and
  87. * max_addr range, where the end address is misaligned:
  88. *
  89. * + + +
  90. * | + +---------+ + |
  91. * | | | rgn | | |
  92. * +------+-------+---------+--+----+
  93. * ^ ^ ^
  94. * | | |
  95. * min_add | max_addr
  96. * |
  97. * Aligned address
  98. * boundary
  99. *
  100. * Expect to allocate an aligned region that ends before max_addr.
  101. */
  102. static int alloc_nid_top_down_end_misaligned_check(void)
  103. {
  104. struct memblock_region *rgn = &memblock.reserved.regions[0];
  105. void *allocated_ptr = NULL;
  106. phys_addr_t size = SZ_128;
  107. phys_addr_t misalign = SZ_2;
  108. phys_addr_t min_addr;
  109. phys_addr_t max_addr;
  110. phys_addr_t rgn_end;
  111. PREFIX_PUSH();
  112. setup_memblock();
  113. min_addr = memblock_start_of_DRAM() + SMP_CACHE_BYTES * 2;
  114. max_addr = min_addr + SZ_512 + misalign;
  115. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  116. min_addr, max_addr,
  117. NUMA_NO_NODE);
  118. rgn_end = rgn->base + rgn->size;
  119. ASSERT_NE(allocated_ptr, NULL);
  120. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  121. ASSERT_EQ(rgn->size, size);
  122. ASSERT_EQ(rgn->base, max_addr - size - misalign);
  123. ASSERT_LT(rgn_end, max_addr);
  124. ASSERT_EQ(memblock.reserved.cnt, 1);
  125. ASSERT_EQ(memblock.reserved.total_size, size);
  126. test_pass_pop();
  127. return 0;
  128. }
  129. /*
  130. * A simple test that tries to allocate a memory region, which spans over the
  131. * min_addr and max_addr range:
  132. *
  133. * + +
  134. * | +---------------+ |
  135. * | | rgn | |
  136. * +------+---------------+-------+
  137. * ^ ^
  138. * | |
  139. * min_addr max_addr
  140. *
  141. * Expect to allocate a region that starts at min_addr and ends at
  142. * max_addr, given that min_addr is aligned.
  143. */
  144. static int alloc_nid_exact_address_generic_check(void)
  145. {
  146. struct memblock_region *rgn = &memblock.reserved.regions[0];
  147. void *allocated_ptr = NULL;
  148. phys_addr_t size = SZ_1K;
  149. phys_addr_t min_addr;
  150. phys_addr_t max_addr;
  151. phys_addr_t rgn_end;
  152. PREFIX_PUSH();
  153. setup_memblock();
  154. min_addr = memblock_start_of_DRAM() + SMP_CACHE_BYTES;
  155. max_addr = min_addr + size;
  156. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  157. min_addr, max_addr,
  158. NUMA_NO_NODE);
  159. rgn_end = rgn->base + rgn->size;
  160. ASSERT_NE(allocated_ptr, NULL);
  161. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  162. ASSERT_EQ(rgn->size, size);
  163. ASSERT_EQ(rgn->base, min_addr);
  164. ASSERT_EQ(rgn_end, max_addr);
  165. ASSERT_EQ(memblock.reserved.cnt, 1);
  166. ASSERT_EQ(memblock.reserved.total_size, size);
  167. test_pass_pop();
  168. return 0;
  169. }
  170. /*
  171. * A test that tries to allocate a memory region, which can't fit into
  172. * min_addr and max_addr range:
  173. *
  174. * + + +
  175. * | +----------+-----+ |
  176. * | | rgn + | |
  177. * +--------+----------+-----+----+
  178. * ^ ^ ^
  179. * | | |
  180. * Aligned | max_addr
  181. * address |
  182. * boundary min_add
  183. *
  184. * Expect to drop the lower limit and allocate a memory region which
  185. * ends at max_addr (if the address is aligned).
  186. */
  187. static int alloc_nid_top_down_narrow_range_check(void)
  188. {
  189. struct memblock_region *rgn = &memblock.reserved.regions[0];
  190. void *allocated_ptr = NULL;
  191. phys_addr_t size = SZ_256;
  192. phys_addr_t min_addr;
  193. phys_addr_t max_addr;
  194. PREFIX_PUSH();
  195. setup_memblock();
  196. min_addr = memblock_start_of_DRAM() + SZ_512;
  197. max_addr = min_addr + SMP_CACHE_BYTES;
  198. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  199. min_addr, max_addr,
  200. NUMA_NO_NODE);
  201. ASSERT_NE(allocated_ptr, NULL);
  202. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  203. ASSERT_EQ(rgn->size, size);
  204. ASSERT_EQ(rgn->base, max_addr - size);
  205. ASSERT_EQ(memblock.reserved.cnt, 1);
  206. ASSERT_EQ(memblock.reserved.total_size, size);
  207. test_pass_pop();
  208. return 0;
  209. }
  210. /*
  211. * A test that tries to allocate a memory region, which can't fit into
  212. * min_addr and max_addr range, with the latter being too close to the beginning
  213. * of the available memory:
  214. *
  215. * +-------------+
  216. * | new |
  217. * +-------------+
  218. * + +
  219. * | + |
  220. * | | |
  221. * +-------+--------------+
  222. * ^ ^
  223. * | |
  224. * | max_addr
  225. * |
  226. * min_addr
  227. *
  228. * Expect no allocation to happen.
  229. */
  230. static int alloc_nid_low_max_generic_check(void)
  231. {
  232. void *allocated_ptr = NULL;
  233. phys_addr_t size = SZ_1K;
  234. phys_addr_t min_addr;
  235. phys_addr_t max_addr;
  236. PREFIX_PUSH();
  237. setup_memblock();
  238. min_addr = memblock_start_of_DRAM();
  239. max_addr = min_addr + SMP_CACHE_BYTES;
  240. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  241. min_addr, max_addr,
  242. NUMA_NO_NODE);
  243. ASSERT_EQ(allocated_ptr, NULL);
  244. test_pass_pop();
  245. return 0;
  246. }
  247. /*
  248. * A test that tries to allocate a memory region within min_addr min_addr range,
  249. * with min_addr being so close that it's next to an allocated region:
  250. *
  251. * + +
  252. * | +--------+---------------|
  253. * | | r1 | rgn |
  254. * +-------+--------+---------------+
  255. * ^ ^
  256. * | |
  257. * min_addr max_addr
  258. *
  259. * Expect a merge of both regions. Only the region size gets updated.
  260. */
  261. static int alloc_nid_min_reserved_generic_check(void)
  262. {
  263. struct memblock_region *rgn = &memblock.reserved.regions[0];
  264. void *allocated_ptr = NULL;
  265. phys_addr_t r1_size = SZ_128;
  266. phys_addr_t r2_size = SZ_64;
  267. phys_addr_t total_size = r1_size + r2_size;
  268. phys_addr_t min_addr;
  269. phys_addr_t max_addr;
  270. phys_addr_t reserved_base;
  271. PREFIX_PUSH();
  272. setup_memblock();
  273. max_addr = memblock_end_of_DRAM();
  274. min_addr = max_addr - r2_size;
  275. reserved_base = min_addr - r1_size;
  276. memblock_reserve_kern(reserved_base, r1_size);
  277. allocated_ptr = run_memblock_alloc_nid(r2_size, SMP_CACHE_BYTES,
  278. min_addr, max_addr,
  279. NUMA_NO_NODE);
  280. ASSERT_NE(allocated_ptr, NULL);
  281. assert_mem_content(allocated_ptr, r2_size, alloc_nid_test_flags);
  282. ASSERT_EQ(rgn->size, total_size);
  283. ASSERT_EQ(rgn->base, reserved_base);
  284. ASSERT_EQ(memblock.reserved.cnt, 1);
  285. ASSERT_EQ(memblock.reserved.total_size, total_size);
  286. test_pass_pop();
  287. return 0;
  288. }
  289. /*
  290. * A test that tries to allocate a memory region within min_addr and max_addr,
  291. * with max_addr being so close that it's next to an allocated region:
  292. *
  293. * + +
  294. * | +-------------+--------|
  295. * | | rgn | r1 |
  296. * +----------+-------------+--------+
  297. * ^ ^
  298. * | |
  299. * min_addr max_addr
  300. *
  301. * Expect a merge of regions. Only the region size gets updated.
  302. */
  303. static int alloc_nid_max_reserved_generic_check(void)
  304. {
  305. struct memblock_region *rgn = &memblock.reserved.regions[0];
  306. void *allocated_ptr = NULL;
  307. phys_addr_t r1_size = SZ_64;
  308. phys_addr_t r2_size = SZ_128;
  309. phys_addr_t total_size = r1_size + r2_size;
  310. phys_addr_t min_addr;
  311. phys_addr_t max_addr;
  312. PREFIX_PUSH();
  313. setup_memblock();
  314. max_addr = memblock_end_of_DRAM() - r1_size;
  315. min_addr = max_addr - r2_size;
  316. memblock_reserve_kern(max_addr, r1_size);
  317. allocated_ptr = run_memblock_alloc_nid(r2_size, SMP_CACHE_BYTES,
  318. min_addr, max_addr,
  319. NUMA_NO_NODE);
  320. ASSERT_NE(allocated_ptr, NULL);
  321. assert_mem_content(allocated_ptr, r2_size, alloc_nid_test_flags);
  322. ASSERT_EQ(rgn->size, total_size);
  323. ASSERT_EQ(rgn->base, min_addr);
  324. ASSERT_EQ(memblock.reserved.cnt, 1);
  325. ASSERT_EQ(memblock.reserved.total_size, total_size);
  326. test_pass_pop();
  327. return 0;
  328. }
  329. /*
  330. * A test that tries to allocate memory within min_addr and max_add range, when
  331. * there are two reserved regions at the borders, with a gap big enough to fit
  332. * a new region:
  333. *
  334. * + +
  335. * | +--------+ +-------+------+ |
  336. * | | r2 | | rgn | r1 | |
  337. * +----+--------+---+-------+------+--+
  338. * ^ ^
  339. * | |
  340. * min_addr max_addr
  341. *
  342. * Expect to merge the new region with r1. The second region does not get
  343. * updated. The total size field gets updated.
  344. */
  345. static int alloc_nid_top_down_reserved_with_space_check(void)
  346. {
  347. struct memblock_region *rgn1 = &memblock.reserved.regions[1];
  348. struct memblock_region *rgn2 = &memblock.reserved.regions[0];
  349. void *allocated_ptr = NULL;
  350. struct region r1, r2;
  351. phys_addr_t r3_size = SZ_64;
  352. phys_addr_t gap_size = SMP_CACHE_BYTES;
  353. phys_addr_t total_size;
  354. phys_addr_t max_addr;
  355. phys_addr_t min_addr;
  356. PREFIX_PUSH();
  357. setup_memblock();
  358. r1.base = memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2;
  359. r1.size = SMP_CACHE_BYTES;
  360. r2.size = SZ_128;
  361. r2.base = r1.base - (r3_size + gap_size + r2.size);
  362. total_size = r1.size + r2.size + r3_size;
  363. min_addr = r2.base + r2.size;
  364. max_addr = r1.base;
  365. memblock_reserve_kern(r1.base, r1.size);
  366. memblock_reserve_kern(r2.base, r2.size);
  367. allocated_ptr = run_memblock_alloc_nid(r3_size, SMP_CACHE_BYTES,
  368. min_addr, max_addr,
  369. NUMA_NO_NODE);
  370. ASSERT_NE(allocated_ptr, NULL);
  371. assert_mem_content(allocated_ptr, r3_size, alloc_nid_test_flags);
  372. ASSERT_EQ(rgn1->size, r1.size + r3_size);
  373. ASSERT_EQ(rgn1->base, max_addr - r3_size);
  374. ASSERT_EQ(rgn2->size, r2.size);
  375. ASSERT_EQ(rgn2->base, r2.base);
  376. ASSERT_EQ(memblock.reserved.cnt, 2);
  377. ASSERT_EQ(memblock.reserved.total_size, total_size);
  378. test_pass_pop();
  379. return 0;
  380. }
  381. /*
  382. * A test that tries to allocate memory within min_addr and max_add range, when
  383. * there are two reserved regions at the borders, with a gap of a size equal to
  384. * the size of the new region:
  385. *
  386. * + +
  387. * | +--------+--------+--------+ |
  388. * | | r2 | r3 | r1 | |
  389. * +-----+--------+--------+--------+-----+
  390. * ^ ^
  391. * | |
  392. * min_addr max_addr
  393. *
  394. * Expect to merge all of the regions into one. The region counter and total
  395. * size fields get updated.
  396. */
  397. static int alloc_nid_reserved_full_merge_generic_check(void)
  398. {
  399. struct memblock_region *rgn = &memblock.reserved.regions[0];
  400. void *allocated_ptr = NULL;
  401. struct region r1, r2;
  402. phys_addr_t r3_size = SZ_64;
  403. phys_addr_t total_size;
  404. phys_addr_t max_addr;
  405. phys_addr_t min_addr;
  406. PREFIX_PUSH();
  407. setup_memblock();
  408. r1.base = memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2;
  409. r1.size = SMP_CACHE_BYTES;
  410. r2.size = SZ_128;
  411. r2.base = r1.base - (r3_size + r2.size);
  412. total_size = r1.size + r2.size + r3_size;
  413. min_addr = r2.base + r2.size;
  414. max_addr = r1.base;
  415. memblock_reserve_kern(r1.base, r1.size);
  416. memblock_reserve_kern(r2.base, r2.size);
  417. allocated_ptr = run_memblock_alloc_nid(r3_size, SMP_CACHE_BYTES,
  418. min_addr, max_addr,
  419. NUMA_NO_NODE);
  420. ASSERT_NE(allocated_ptr, NULL);
  421. assert_mem_content(allocated_ptr, r3_size, alloc_nid_test_flags);
  422. ASSERT_EQ(rgn->size, total_size);
  423. ASSERT_EQ(rgn->base, r2.base);
  424. ASSERT_EQ(memblock.reserved.cnt, 1);
  425. ASSERT_EQ(memblock.reserved.total_size, total_size);
  426. test_pass_pop();
  427. return 0;
  428. }
  429. /*
  430. * A test that tries to allocate memory within min_addr and max_add range, when
  431. * there are two reserved regions at the borders, with a gap that can't fit
  432. * a new region:
  433. *
  434. * + +
  435. * | +----------+------+ +------+ |
  436. * | | r3 | r2 | | r1 | |
  437. * +--+----------+------+----+------+---+
  438. * ^ ^
  439. * | |
  440. * | max_addr
  441. * |
  442. * min_addr
  443. *
  444. * Expect to merge the new region with r2. The second region does not get
  445. * updated. The total size counter gets updated.
  446. */
  447. static int alloc_nid_top_down_reserved_no_space_check(void)
  448. {
  449. struct memblock_region *rgn1 = &memblock.reserved.regions[1];
  450. struct memblock_region *rgn2 = &memblock.reserved.regions[0];
  451. void *allocated_ptr = NULL;
  452. struct region r1, r2;
  453. phys_addr_t r3_size = SZ_256;
  454. phys_addr_t gap_size = SMP_CACHE_BYTES;
  455. phys_addr_t total_size;
  456. phys_addr_t max_addr;
  457. phys_addr_t min_addr;
  458. PREFIX_PUSH();
  459. setup_memblock();
  460. r1.base = memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2;
  461. r1.size = SMP_CACHE_BYTES;
  462. r2.size = SZ_128;
  463. r2.base = r1.base - (r2.size + gap_size);
  464. total_size = r1.size + r2.size + r3_size;
  465. min_addr = r2.base + r2.size;
  466. max_addr = r1.base;
  467. memblock_reserve_kern(r1.base, r1.size);
  468. memblock_reserve_kern(r2.base, r2.size);
  469. allocated_ptr = run_memblock_alloc_nid(r3_size, SMP_CACHE_BYTES,
  470. min_addr, max_addr,
  471. NUMA_NO_NODE);
  472. ASSERT_NE(allocated_ptr, NULL);
  473. assert_mem_content(allocated_ptr, r3_size, alloc_nid_test_flags);
  474. ASSERT_EQ(rgn1->size, r1.size);
  475. ASSERT_EQ(rgn1->base, r1.base);
  476. ASSERT_EQ(rgn2->size, r2.size + r3_size);
  477. ASSERT_EQ(rgn2->base, r2.base - r3_size);
  478. ASSERT_EQ(memblock.reserved.cnt, 2);
  479. ASSERT_EQ(memblock.reserved.total_size, total_size);
  480. test_pass_pop();
  481. return 0;
  482. }
  483. /*
  484. * A test that tries to allocate memory within min_addr and max_add range, but
  485. * it's too narrow and everything else is reserved:
  486. *
  487. * +-----------+
  488. * | new |
  489. * +-----------+
  490. * + +
  491. * |--------------+ +----------|
  492. * | r2 | | r1 |
  493. * +--------------+------+----------+
  494. * ^ ^
  495. * | |
  496. * | max_addr
  497. * |
  498. * min_addr
  499. *
  500. * Expect no allocation to happen.
  501. */
  502. static int alloc_nid_reserved_all_generic_check(void)
  503. {
  504. void *allocated_ptr = NULL;
  505. struct region r1, r2;
  506. phys_addr_t r3_size = SZ_256;
  507. phys_addr_t gap_size = SMP_CACHE_BYTES;
  508. phys_addr_t max_addr;
  509. phys_addr_t min_addr;
  510. PREFIX_PUSH();
  511. setup_memblock();
  512. r1.base = memblock_end_of_DRAM() - SMP_CACHE_BYTES;
  513. r1.size = SMP_CACHE_BYTES;
  514. r2.size = MEM_SIZE - (r1.size + gap_size);
  515. r2.base = memblock_start_of_DRAM();
  516. min_addr = r2.base + r2.size;
  517. max_addr = r1.base;
  518. memblock_reserve(r1.base, r1.size);
  519. memblock_reserve(r2.base, r2.size);
  520. allocated_ptr = run_memblock_alloc_nid(r3_size, SMP_CACHE_BYTES,
  521. min_addr, max_addr,
  522. NUMA_NO_NODE);
  523. ASSERT_EQ(allocated_ptr, NULL);
  524. test_pass_pop();
  525. return 0;
  526. }
  527. /*
  528. * A test that tries to allocate a memory region, where max_addr is
  529. * bigger than the end address of the available memory. Expect to allocate
  530. * a region that ends before the end of the memory.
  531. */
  532. static int alloc_nid_top_down_cap_max_check(void)
  533. {
  534. struct memblock_region *rgn = &memblock.reserved.regions[0];
  535. void *allocated_ptr = NULL;
  536. phys_addr_t size = SZ_256;
  537. phys_addr_t min_addr;
  538. phys_addr_t max_addr;
  539. PREFIX_PUSH();
  540. setup_memblock();
  541. min_addr = memblock_end_of_DRAM() - SZ_1K;
  542. max_addr = memblock_end_of_DRAM() + SZ_256;
  543. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  544. min_addr, max_addr,
  545. NUMA_NO_NODE);
  546. ASSERT_NE(allocated_ptr, NULL);
  547. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  548. ASSERT_EQ(rgn->size, size);
  549. ASSERT_EQ(rgn->base, memblock_end_of_DRAM() - size);
  550. ASSERT_EQ(memblock.reserved.cnt, 1);
  551. ASSERT_EQ(memblock.reserved.total_size, size);
  552. test_pass_pop();
  553. return 0;
  554. }
  555. /*
  556. * A test that tries to allocate a memory region, where min_addr is
  557. * smaller than the start address of the available memory. Expect to allocate
  558. * a region that ends before the end of the memory.
  559. */
  560. static int alloc_nid_top_down_cap_min_check(void)
  561. {
  562. struct memblock_region *rgn = &memblock.reserved.regions[0];
  563. void *allocated_ptr = NULL;
  564. phys_addr_t size = SZ_1K;
  565. phys_addr_t min_addr;
  566. phys_addr_t max_addr;
  567. PREFIX_PUSH();
  568. setup_memblock();
  569. min_addr = memblock_start_of_DRAM() - SZ_256;
  570. max_addr = memblock_end_of_DRAM();
  571. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  572. min_addr, max_addr,
  573. NUMA_NO_NODE);
  574. ASSERT_NE(allocated_ptr, NULL);
  575. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  576. ASSERT_EQ(rgn->size, size);
  577. ASSERT_EQ(rgn->base, memblock_end_of_DRAM() - size);
  578. ASSERT_EQ(memblock.reserved.cnt, 1);
  579. ASSERT_EQ(memblock.reserved.total_size, size);
  580. test_pass_pop();
  581. return 0;
  582. }
  583. /*
  584. * A simple test that tries to allocate a memory region within min_addr and
  585. * max_addr range:
  586. *
  587. * + +
  588. * | +-----------+ | |
  589. * | | rgn | | |
  590. * +----+-----------+-----------+------+
  591. * ^ ^
  592. * | |
  593. * min_addr max_addr
  594. *
  595. * Expect to allocate a region that ends before max_addr.
  596. */
  597. static int alloc_nid_bottom_up_simple_check(void)
  598. {
  599. struct memblock_region *rgn = &memblock.reserved.regions[0];
  600. void *allocated_ptr = NULL;
  601. phys_addr_t size = SZ_128;
  602. phys_addr_t min_addr;
  603. phys_addr_t max_addr;
  604. phys_addr_t rgn_end;
  605. PREFIX_PUSH();
  606. setup_memblock();
  607. min_addr = memblock_start_of_DRAM() + SMP_CACHE_BYTES * 2;
  608. max_addr = min_addr + SZ_512;
  609. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  610. min_addr, max_addr,
  611. NUMA_NO_NODE);
  612. rgn_end = rgn->base + rgn->size;
  613. ASSERT_NE(allocated_ptr, NULL);
  614. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  615. ASSERT_EQ(rgn->size, size);
  616. ASSERT_EQ(rgn->base, min_addr);
  617. ASSERT_LT(rgn_end, max_addr);
  618. ASSERT_EQ(memblock.reserved.cnt, 1);
  619. ASSERT_EQ(memblock.reserved.total_size, size);
  620. test_pass_pop();
  621. return 0;
  622. }
  623. /*
  624. * A simple test that tries to allocate a memory region within min_addr and
  625. * max_addr range, where the start address is misaligned:
  626. *
  627. * + +
  628. * | + +-----------+ + |
  629. * | | | rgn | | |
  630. * +-----+---+-----------+-----+-----+
  631. * ^ ^----. ^
  632. * | | |
  633. * min_add | max_addr
  634. * |
  635. * Aligned address
  636. * boundary
  637. *
  638. * Expect to allocate an aligned region that ends before max_addr.
  639. */
  640. static int alloc_nid_bottom_up_start_misaligned_check(void)
  641. {
  642. struct memblock_region *rgn = &memblock.reserved.regions[0];
  643. void *allocated_ptr = NULL;
  644. phys_addr_t size = SZ_128;
  645. phys_addr_t misalign = SZ_2;
  646. phys_addr_t min_addr;
  647. phys_addr_t max_addr;
  648. phys_addr_t rgn_end;
  649. PREFIX_PUSH();
  650. setup_memblock();
  651. min_addr = memblock_start_of_DRAM() + misalign;
  652. max_addr = min_addr + SZ_512;
  653. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  654. min_addr, max_addr,
  655. NUMA_NO_NODE);
  656. rgn_end = rgn->base + rgn->size;
  657. ASSERT_NE(allocated_ptr, NULL);
  658. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  659. ASSERT_EQ(rgn->size, size);
  660. ASSERT_EQ(rgn->base, min_addr + (SMP_CACHE_BYTES - misalign));
  661. ASSERT_LT(rgn_end, max_addr);
  662. ASSERT_EQ(memblock.reserved.cnt, 1);
  663. ASSERT_EQ(memblock.reserved.total_size, size);
  664. test_pass_pop();
  665. return 0;
  666. }
  667. /*
  668. * A test that tries to allocate a memory region, which can't fit into min_addr
  669. * and max_addr range:
  670. *
  671. * + +
  672. * |---------+ + + |
  673. * | rgn | | | |
  674. * +---------+---------+----+------+
  675. * ^ ^
  676. * | |
  677. * | max_addr
  678. * |
  679. * min_add
  680. *
  681. * Expect to drop the lower limit and allocate a memory region which
  682. * starts at the beginning of the available memory.
  683. */
  684. static int alloc_nid_bottom_up_narrow_range_check(void)
  685. {
  686. struct memblock_region *rgn = &memblock.reserved.regions[0];
  687. void *allocated_ptr = NULL;
  688. phys_addr_t size = SZ_256;
  689. phys_addr_t min_addr;
  690. phys_addr_t max_addr;
  691. PREFIX_PUSH();
  692. setup_memblock();
  693. min_addr = memblock_start_of_DRAM() + SZ_512;
  694. max_addr = min_addr + SMP_CACHE_BYTES;
  695. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  696. min_addr, max_addr,
  697. NUMA_NO_NODE);
  698. ASSERT_NE(allocated_ptr, NULL);
  699. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  700. ASSERT_EQ(rgn->size, size);
  701. ASSERT_EQ(rgn->base, memblock_start_of_DRAM());
  702. ASSERT_EQ(memblock.reserved.cnt, 1);
  703. ASSERT_EQ(memblock.reserved.total_size, size);
  704. test_pass_pop();
  705. return 0;
  706. }
  707. /*
  708. * A test that tries to allocate memory within min_addr and max_add range, when
  709. * there are two reserved regions at the borders, with a gap big enough to fit
  710. * a new region:
  711. *
  712. * + +
  713. * | +--------+-------+ +------+ |
  714. * | | r2 | rgn | | r1 | |
  715. * +----+--------+-------+---+------+--+
  716. * ^ ^
  717. * | |
  718. * min_addr max_addr
  719. *
  720. * Expect to merge the new region with r2. The second region does not get
  721. * updated. The total size field gets updated.
  722. */
  723. static int alloc_nid_bottom_up_reserved_with_space_check(void)
  724. {
  725. struct memblock_region *rgn1 = &memblock.reserved.regions[1];
  726. struct memblock_region *rgn2 = &memblock.reserved.regions[0];
  727. void *allocated_ptr = NULL;
  728. struct region r1, r2;
  729. phys_addr_t r3_size = SZ_64;
  730. phys_addr_t gap_size = SMP_CACHE_BYTES;
  731. phys_addr_t total_size;
  732. phys_addr_t max_addr;
  733. phys_addr_t min_addr;
  734. PREFIX_PUSH();
  735. setup_memblock();
  736. r1.base = memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2;
  737. r1.size = SMP_CACHE_BYTES;
  738. r2.size = SZ_128;
  739. r2.base = r1.base - (r3_size + gap_size + r2.size);
  740. total_size = r1.size + r2.size + r3_size;
  741. min_addr = r2.base + r2.size;
  742. max_addr = r1.base;
  743. memblock_reserve_kern(r1.base, r1.size);
  744. memblock_reserve_kern(r2.base, r2.size);
  745. allocated_ptr = run_memblock_alloc_nid(r3_size, SMP_CACHE_BYTES,
  746. min_addr, max_addr,
  747. NUMA_NO_NODE);
  748. ASSERT_NE(allocated_ptr, NULL);
  749. assert_mem_content(allocated_ptr, r3_size, alloc_nid_test_flags);
  750. ASSERT_EQ(rgn1->size, r1.size);
  751. ASSERT_EQ(rgn1->base, max_addr);
  752. ASSERT_EQ(rgn2->size, r2.size + r3_size);
  753. ASSERT_EQ(rgn2->base, r2.base);
  754. ASSERT_EQ(memblock.reserved.cnt, 2);
  755. ASSERT_EQ(memblock.reserved.total_size, total_size);
  756. test_pass_pop();
  757. return 0;
  758. }
  759. /*
  760. * A test that tries to allocate memory within min_addr and max_add range, when
  761. * there are two reserved regions at the borders, with a gap of a size equal to
  762. * the size of the new region:
  763. *
  764. * + +
  765. * |----------+ +------+ +----+ |
  766. * | r3 | | r2 | | r1 | |
  767. * +----------+----+------+---+----+--+
  768. * ^ ^
  769. * | |
  770. * | max_addr
  771. * |
  772. * min_addr
  773. *
  774. * Expect to drop the lower limit and allocate memory at the beginning of the
  775. * available memory. The region counter and total size fields get updated.
  776. * Other regions are not modified.
  777. */
  778. static int alloc_nid_bottom_up_reserved_no_space_check(void)
  779. {
  780. struct memblock_region *rgn1 = &memblock.reserved.regions[2];
  781. struct memblock_region *rgn2 = &memblock.reserved.regions[1];
  782. struct memblock_region *rgn3 = &memblock.reserved.regions[0];
  783. void *allocated_ptr = NULL;
  784. struct region r1, r2;
  785. phys_addr_t r3_size = SZ_256;
  786. phys_addr_t gap_size = SMP_CACHE_BYTES;
  787. phys_addr_t total_size;
  788. phys_addr_t max_addr;
  789. phys_addr_t min_addr;
  790. PREFIX_PUSH();
  791. setup_memblock();
  792. r1.base = memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2;
  793. r1.size = SMP_CACHE_BYTES;
  794. r2.size = SZ_128;
  795. r2.base = r1.base - (r2.size + gap_size);
  796. total_size = r1.size + r2.size + r3_size;
  797. min_addr = r2.base + r2.size;
  798. max_addr = r1.base;
  799. memblock_reserve(r1.base, r1.size);
  800. memblock_reserve(r2.base, r2.size);
  801. allocated_ptr = run_memblock_alloc_nid(r3_size, SMP_CACHE_BYTES,
  802. min_addr, max_addr,
  803. NUMA_NO_NODE);
  804. ASSERT_NE(allocated_ptr, NULL);
  805. assert_mem_content(allocated_ptr, r3_size, alloc_nid_test_flags);
  806. ASSERT_EQ(rgn3->size, r3_size);
  807. ASSERT_EQ(rgn3->base, memblock_start_of_DRAM());
  808. ASSERT_EQ(rgn2->size, r2.size);
  809. ASSERT_EQ(rgn2->base, r2.base);
  810. ASSERT_EQ(rgn1->size, r1.size);
  811. ASSERT_EQ(rgn1->base, r1.base);
  812. ASSERT_EQ(memblock.reserved.cnt, 3);
  813. ASSERT_EQ(memblock.reserved.total_size, total_size);
  814. test_pass_pop();
  815. return 0;
  816. }
  817. /*
  818. * A test that tries to allocate a memory region, where max_addr is
  819. * bigger than the end address of the available memory. Expect to allocate
  820. * a region that starts at the min_addr.
  821. */
  822. static int alloc_nid_bottom_up_cap_max_check(void)
  823. {
  824. struct memblock_region *rgn = &memblock.reserved.regions[0];
  825. void *allocated_ptr = NULL;
  826. phys_addr_t size = SZ_256;
  827. phys_addr_t min_addr;
  828. phys_addr_t max_addr;
  829. PREFIX_PUSH();
  830. setup_memblock();
  831. min_addr = memblock_start_of_DRAM() + SZ_1K;
  832. max_addr = memblock_end_of_DRAM() + SZ_256;
  833. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  834. min_addr, max_addr,
  835. NUMA_NO_NODE);
  836. ASSERT_NE(allocated_ptr, NULL);
  837. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  838. ASSERT_EQ(rgn->size, size);
  839. ASSERT_EQ(rgn->base, min_addr);
  840. ASSERT_EQ(memblock.reserved.cnt, 1);
  841. ASSERT_EQ(memblock.reserved.total_size, size);
  842. test_pass_pop();
  843. return 0;
  844. }
  845. /*
  846. * A test that tries to allocate a memory region, where min_addr is
  847. * smaller than the start address of the available memory. Expect to allocate
  848. * a region at the beginning of the available memory.
  849. */
  850. static int alloc_nid_bottom_up_cap_min_check(void)
  851. {
  852. struct memblock_region *rgn = &memblock.reserved.regions[0];
  853. void *allocated_ptr = NULL;
  854. phys_addr_t size = SZ_1K;
  855. phys_addr_t min_addr;
  856. phys_addr_t max_addr;
  857. PREFIX_PUSH();
  858. setup_memblock();
  859. min_addr = memblock_start_of_DRAM();
  860. max_addr = memblock_end_of_DRAM() - SZ_256;
  861. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  862. min_addr, max_addr,
  863. NUMA_NO_NODE);
  864. ASSERT_NE(allocated_ptr, NULL);
  865. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  866. ASSERT_EQ(rgn->size, size);
  867. ASSERT_EQ(rgn->base, memblock_start_of_DRAM());
  868. ASSERT_EQ(memblock.reserved.cnt, 1);
  869. ASSERT_EQ(memblock.reserved.total_size, size);
  870. test_pass_pop();
  871. return 0;
  872. }
  873. /* Test case wrappers for range tests */
  874. static int alloc_nid_simple_check(void)
  875. {
  876. test_print("\tRunning %s...\n", __func__);
  877. memblock_set_bottom_up(false);
  878. alloc_nid_top_down_simple_check();
  879. memblock_set_bottom_up(true);
  880. alloc_nid_bottom_up_simple_check();
  881. return 0;
  882. }
  883. static int alloc_nid_misaligned_check(void)
  884. {
  885. test_print("\tRunning %s...\n", __func__);
  886. memblock_set_bottom_up(false);
  887. alloc_nid_top_down_end_misaligned_check();
  888. memblock_set_bottom_up(true);
  889. alloc_nid_bottom_up_start_misaligned_check();
  890. return 0;
  891. }
  892. static int alloc_nid_narrow_range_check(void)
  893. {
  894. test_print("\tRunning %s...\n", __func__);
  895. memblock_set_bottom_up(false);
  896. alloc_nid_top_down_narrow_range_check();
  897. memblock_set_bottom_up(true);
  898. alloc_nid_bottom_up_narrow_range_check();
  899. return 0;
  900. }
  901. static int alloc_nid_reserved_with_space_check(void)
  902. {
  903. test_print("\tRunning %s...\n", __func__);
  904. memblock_set_bottom_up(false);
  905. alloc_nid_top_down_reserved_with_space_check();
  906. memblock_set_bottom_up(true);
  907. alloc_nid_bottom_up_reserved_with_space_check();
  908. return 0;
  909. }
  910. static int alloc_nid_reserved_no_space_check(void)
  911. {
  912. test_print("\tRunning %s...\n", __func__);
  913. memblock_set_bottom_up(false);
  914. alloc_nid_top_down_reserved_no_space_check();
  915. memblock_set_bottom_up(true);
  916. alloc_nid_bottom_up_reserved_no_space_check();
  917. return 0;
  918. }
  919. static int alloc_nid_cap_max_check(void)
  920. {
  921. test_print("\tRunning %s...\n", __func__);
  922. memblock_set_bottom_up(false);
  923. alloc_nid_top_down_cap_max_check();
  924. memblock_set_bottom_up(true);
  925. alloc_nid_bottom_up_cap_max_check();
  926. return 0;
  927. }
  928. static int alloc_nid_cap_min_check(void)
  929. {
  930. test_print("\tRunning %s...\n", __func__);
  931. memblock_set_bottom_up(false);
  932. alloc_nid_top_down_cap_min_check();
  933. memblock_set_bottom_up(true);
  934. alloc_nid_bottom_up_cap_min_check();
  935. return 0;
  936. }
  937. static int alloc_nid_min_reserved_check(void)
  938. {
  939. test_print("\tRunning %s...\n", __func__);
  940. run_top_down(alloc_nid_min_reserved_generic_check);
  941. run_bottom_up(alloc_nid_min_reserved_generic_check);
  942. return 0;
  943. }
  944. static int alloc_nid_max_reserved_check(void)
  945. {
  946. test_print("\tRunning %s...\n", __func__);
  947. run_top_down(alloc_nid_max_reserved_generic_check);
  948. run_bottom_up(alloc_nid_max_reserved_generic_check);
  949. return 0;
  950. }
  951. static int alloc_nid_exact_address_check(void)
  952. {
  953. test_print("\tRunning %s...\n", __func__);
  954. run_top_down(alloc_nid_exact_address_generic_check);
  955. run_bottom_up(alloc_nid_exact_address_generic_check);
  956. return 0;
  957. }
  958. static int alloc_nid_reserved_full_merge_check(void)
  959. {
  960. test_print("\tRunning %s...\n", __func__);
  961. run_top_down(alloc_nid_reserved_full_merge_generic_check);
  962. run_bottom_up(alloc_nid_reserved_full_merge_generic_check);
  963. return 0;
  964. }
  965. static int alloc_nid_reserved_all_check(void)
  966. {
  967. test_print("\tRunning %s...\n", __func__);
  968. run_top_down(alloc_nid_reserved_all_generic_check);
  969. run_bottom_up(alloc_nid_reserved_all_generic_check);
  970. return 0;
  971. }
  972. static int alloc_nid_low_max_check(void)
  973. {
  974. test_print("\tRunning %s...\n", __func__);
  975. run_top_down(alloc_nid_low_max_generic_check);
  976. run_bottom_up(alloc_nid_low_max_generic_check);
  977. return 0;
  978. }
  979. static int memblock_alloc_nid_range_checks(void)
  980. {
  981. test_print("Running %s range tests...\n",
  982. get_memblock_alloc_nid_name(alloc_nid_test_flags));
  983. alloc_nid_simple_check();
  984. alloc_nid_misaligned_check();
  985. alloc_nid_narrow_range_check();
  986. alloc_nid_reserved_with_space_check();
  987. alloc_nid_reserved_no_space_check();
  988. alloc_nid_cap_max_check();
  989. alloc_nid_cap_min_check();
  990. alloc_nid_min_reserved_check();
  991. alloc_nid_max_reserved_check();
  992. alloc_nid_exact_address_check();
  993. alloc_nid_reserved_full_merge_check();
  994. alloc_nid_reserved_all_check();
  995. alloc_nid_low_max_check();
  996. return 0;
  997. }
  998. /*
  999. * A test that tries to allocate a memory region in a specific NUMA node that
  1000. * has enough memory to allocate a region of the requested size.
  1001. * Expect to allocate an aligned region at the end of the requested node.
  1002. */
  1003. static int alloc_nid_top_down_numa_simple_check(void)
  1004. {
  1005. int nid_req = 3;
  1006. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1007. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1008. void *allocated_ptr = NULL;
  1009. phys_addr_t size;
  1010. phys_addr_t min_addr;
  1011. phys_addr_t max_addr;
  1012. PREFIX_PUSH();
  1013. setup_numa_memblock(node_fractions);
  1014. ASSERT_LE(SZ_4, req_node->size);
  1015. size = req_node->size / SZ_4;
  1016. min_addr = memblock_start_of_DRAM();
  1017. max_addr = memblock_end_of_DRAM();
  1018. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1019. min_addr, max_addr, nid_req);
  1020. ASSERT_NE(allocated_ptr, NULL);
  1021. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1022. ASSERT_EQ(new_rgn->size, size);
  1023. ASSERT_EQ(new_rgn->base, region_end(req_node) - size);
  1024. ASSERT_LE(req_node->base, new_rgn->base);
  1025. ASSERT_EQ(memblock.reserved.cnt, 1);
  1026. ASSERT_EQ(memblock.reserved.total_size, size);
  1027. test_pass_pop();
  1028. return 0;
  1029. }
  1030. /*
  1031. * A test that tries to allocate a memory region in a specific NUMA node that
  1032. * does not have enough memory to allocate a region of the requested size:
  1033. *
  1034. * | +-----+ +------------------+ |
  1035. * | | req | | expected | |
  1036. * +---+-----+----------+------------------+-----+
  1037. *
  1038. * | +---------+ |
  1039. * | | rgn | |
  1040. * +-----------------------------+---------+-----+
  1041. *
  1042. * Expect to allocate an aligned region at the end of the last node that has
  1043. * enough memory (in this case, nid = 6) after falling back to NUMA_NO_NODE.
  1044. */
  1045. static int alloc_nid_top_down_numa_small_node_check(void)
  1046. {
  1047. int nid_req = 1;
  1048. int nid_exp = 6;
  1049. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1050. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1051. struct memblock_region *exp_node = &memblock.memory.regions[nid_exp];
  1052. void *allocated_ptr = NULL;
  1053. phys_addr_t size;
  1054. phys_addr_t min_addr;
  1055. phys_addr_t max_addr;
  1056. PREFIX_PUSH();
  1057. setup_numa_memblock(node_fractions);
  1058. size = SZ_2 * req_node->size;
  1059. min_addr = memblock_start_of_DRAM();
  1060. max_addr = memblock_end_of_DRAM();
  1061. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1062. min_addr, max_addr, nid_req);
  1063. ASSERT_NE(allocated_ptr, NULL);
  1064. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1065. ASSERT_EQ(new_rgn->size, size);
  1066. ASSERT_EQ(new_rgn->base, region_end(exp_node) - size);
  1067. ASSERT_LE(exp_node->base, new_rgn->base);
  1068. ASSERT_EQ(memblock.reserved.cnt, 1);
  1069. ASSERT_EQ(memblock.reserved.total_size, size);
  1070. test_pass_pop();
  1071. return 0;
  1072. }
  1073. /*
  1074. * A test that tries to allocate a memory region in a specific NUMA node that
  1075. * is fully reserved:
  1076. *
  1077. * | +---------+ +------------------+ |
  1078. * | |requested| | expected | |
  1079. * +--------------+---------+------------+------------------+-----+
  1080. *
  1081. * | +---------+ +---------+ |
  1082. * | | reserved| | new | |
  1083. * +--------------+---------+---------------------+---------+-----+
  1084. *
  1085. * Expect to allocate an aligned region at the end of the last node that is
  1086. * large enough and has enough unreserved memory (in this case, nid = 6) after
  1087. * falling back to NUMA_NO_NODE. The region count and total size get updated.
  1088. */
  1089. static int alloc_nid_top_down_numa_node_reserved_check(void)
  1090. {
  1091. int nid_req = 2;
  1092. int nid_exp = 6;
  1093. struct memblock_region *new_rgn = &memblock.reserved.regions[1];
  1094. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1095. struct memblock_region *exp_node = &memblock.memory.regions[nid_exp];
  1096. void *allocated_ptr = NULL;
  1097. phys_addr_t size;
  1098. phys_addr_t min_addr;
  1099. phys_addr_t max_addr;
  1100. PREFIX_PUSH();
  1101. setup_numa_memblock(node_fractions);
  1102. size = req_node->size;
  1103. min_addr = memblock_start_of_DRAM();
  1104. max_addr = memblock_end_of_DRAM();
  1105. memblock_reserve(req_node->base, req_node->size);
  1106. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1107. min_addr, max_addr, nid_req);
  1108. ASSERT_NE(allocated_ptr, NULL);
  1109. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1110. ASSERT_EQ(new_rgn->size, size);
  1111. ASSERT_EQ(new_rgn->base, region_end(exp_node) - size);
  1112. ASSERT_LE(exp_node->base, new_rgn->base);
  1113. ASSERT_EQ(memblock.reserved.cnt, 2);
  1114. ASSERT_EQ(memblock.reserved.total_size, size + req_node->size);
  1115. test_pass_pop();
  1116. return 0;
  1117. }
  1118. /*
  1119. * A test that tries to allocate a memory region in a specific NUMA node that
  1120. * is partially reserved but has enough memory for the allocated region:
  1121. *
  1122. * | +---------------------------------------+ |
  1123. * | | requested | |
  1124. * +-----------+---------------------------------------+----------+
  1125. *
  1126. * | +------------------+ +-----+ |
  1127. * | | reserved | | new | |
  1128. * +-----------+------------------+--------------+-----+----------+
  1129. *
  1130. * Expect to allocate an aligned region at the end of the requested node. The
  1131. * region count and total size get updated.
  1132. */
  1133. static int alloc_nid_top_down_numa_part_reserved_check(void)
  1134. {
  1135. int nid_req = 4;
  1136. struct memblock_region *new_rgn = &memblock.reserved.regions[1];
  1137. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1138. void *allocated_ptr = NULL;
  1139. struct region r1;
  1140. phys_addr_t size;
  1141. phys_addr_t min_addr;
  1142. phys_addr_t max_addr;
  1143. PREFIX_PUSH();
  1144. setup_numa_memblock(node_fractions);
  1145. ASSERT_LE(SZ_8, req_node->size);
  1146. r1.base = req_node->base;
  1147. r1.size = req_node->size / SZ_2;
  1148. size = r1.size / SZ_4;
  1149. min_addr = memblock_start_of_DRAM();
  1150. max_addr = memblock_end_of_DRAM();
  1151. memblock_reserve(r1.base, r1.size);
  1152. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1153. min_addr, max_addr, nid_req);
  1154. ASSERT_NE(allocated_ptr, NULL);
  1155. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1156. ASSERT_EQ(new_rgn->size, size);
  1157. ASSERT_EQ(new_rgn->base, region_end(req_node) - size);
  1158. ASSERT_LE(req_node->base, new_rgn->base);
  1159. ASSERT_EQ(memblock.reserved.cnt, 2);
  1160. ASSERT_EQ(memblock.reserved.total_size, size + r1.size);
  1161. test_pass_pop();
  1162. return 0;
  1163. }
  1164. /*
  1165. * A test that tries to allocate a memory region in a specific NUMA node that
  1166. * is partially reserved and does not have enough contiguous memory for the
  1167. * allocated region:
  1168. *
  1169. * | +-----------------------+ +----------------------|
  1170. * | | requested | | expected |
  1171. * +-----------+-----------------------+---------+----------------------+
  1172. *
  1173. * | +----------+ +-----------|
  1174. * | | reserved | | new |
  1175. * +-----------------+----------+---------------------------+-----------+
  1176. *
  1177. * Expect to allocate an aligned region at the end of the last node that is
  1178. * large enough and has enough unreserved memory (in this case,
  1179. * nid = NUMA_NODES - 1) after falling back to NUMA_NO_NODE. The region count
  1180. * and total size get updated.
  1181. */
  1182. static int alloc_nid_top_down_numa_part_reserved_fallback_check(void)
  1183. {
  1184. int nid_req = 4;
  1185. int nid_exp = NUMA_NODES - 1;
  1186. struct memblock_region *new_rgn = &memblock.reserved.regions[1];
  1187. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1188. struct memblock_region *exp_node = &memblock.memory.regions[nid_exp];
  1189. void *allocated_ptr = NULL;
  1190. struct region r1;
  1191. phys_addr_t size;
  1192. phys_addr_t min_addr;
  1193. phys_addr_t max_addr;
  1194. PREFIX_PUSH();
  1195. setup_numa_memblock(node_fractions);
  1196. ASSERT_LE(SZ_4, req_node->size);
  1197. size = req_node->size / SZ_2;
  1198. r1.base = req_node->base + (size / SZ_2);
  1199. r1.size = size;
  1200. min_addr = memblock_start_of_DRAM();
  1201. max_addr = memblock_end_of_DRAM();
  1202. memblock_reserve(r1.base, r1.size);
  1203. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1204. min_addr, max_addr, nid_req);
  1205. ASSERT_NE(allocated_ptr, NULL);
  1206. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1207. ASSERT_EQ(new_rgn->size, size);
  1208. ASSERT_EQ(new_rgn->base, region_end(exp_node) - size);
  1209. ASSERT_LE(exp_node->base, new_rgn->base);
  1210. ASSERT_EQ(memblock.reserved.cnt, 2);
  1211. ASSERT_EQ(memblock.reserved.total_size, size + r1.size);
  1212. test_pass_pop();
  1213. return 0;
  1214. }
  1215. /*
  1216. * A test that tries to allocate a memory region that spans over the min_addr
  1217. * and max_addr range and overlaps with two different nodes, where the first
  1218. * node is the requested node:
  1219. *
  1220. * min_addr
  1221. * | max_addr
  1222. * | |
  1223. * v v
  1224. * | +-----------------------+-----------+ |
  1225. * | | requested | node3 | |
  1226. * +-----------+-----------------------+-----------+--------------+
  1227. * + +
  1228. * | +-----------+ |
  1229. * | | rgn | |
  1230. * +-----------------------+-----------+--------------------------+
  1231. *
  1232. * Expect to drop the lower limit and allocate a memory region that ends at
  1233. * the end of the requested node.
  1234. */
  1235. static int alloc_nid_top_down_numa_split_range_low_check(void)
  1236. {
  1237. int nid_req = 2;
  1238. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1239. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1240. void *allocated_ptr = NULL;
  1241. phys_addr_t size = SZ_512;
  1242. phys_addr_t min_addr;
  1243. phys_addr_t max_addr;
  1244. phys_addr_t req_node_end;
  1245. PREFIX_PUSH();
  1246. setup_numa_memblock(node_fractions);
  1247. req_node_end = region_end(req_node);
  1248. min_addr = req_node_end - SZ_256;
  1249. max_addr = min_addr + size;
  1250. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1251. min_addr, max_addr, nid_req);
  1252. ASSERT_NE(allocated_ptr, NULL);
  1253. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1254. ASSERT_EQ(new_rgn->size, size);
  1255. ASSERT_EQ(new_rgn->base, req_node_end - size);
  1256. ASSERT_LE(req_node->base, new_rgn->base);
  1257. ASSERT_EQ(memblock.reserved.cnt, 1);
  1258. ASSERT_EQ(memblock.reserved.total_size, size);
  1259. test_pass_pop();
  1260. return 0;
  1261. }
  1262. /*
  1263. * A test that tries to allocate a memory region that spans over the min_addr
  1264. * and max_addr range and overlaps with two different nodes, where the second
  1265. * node is the requested node:
  1266. *
  1267. * min_addr
  1268. * | max_addr
  1269. * | |
  1270. * v v
  1271. * | +--------------------------+---------+ |
  1272. * | | expected |requested| |
  1273. * +------+--------------------------+---------+----------------+
  1274. * + +
  1275. * | +---------+ |
  1276. * | | rgn | |
  1277. * +-----------------------+---------+--------------------------+
  1278. *
  1279. * Expect to drop the lower limit and allocate a memory region that
  1280. * ends at the end of the first node that overlaps with the range.
  1281. */
  1282. static int alloc_nid_top_down_numa_split_range_high_check(void)
  1283. {
  1284. int nid_req = 3;
  1285. int nid_exp = nid_req - 1;
  1286. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1287. struct memblock_region *exp_node = &memblock.memory.regions[nid_exp];
  1288. void *allocated_ptr = NULL;
  1289. phys_addr_t size = SZ_512;
  1290. phys_addr_t min_addr;
  1291. phys_addr_t max_addr;
  1292. phys_addr_t exp_node_end;
  1293. PREFIX_PUSH();
  1294. setup_numa_memblock(node_fractions);
  1295. exp_node_end = region_end(exp_node);
  1296. min_addr = exp_node_end - SZ_256;
  1297. max_addr = min_addr + size;
  1298. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1299. min_addr, max_addr, nid_req);
  1300. ASSERT_NE(allocated_ptr, NULL);
  1301. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1302. ASSERT_EQ(new_rgn->size, size);
  1303. ASSERT_EQ(new_rgn->base, exp_node_end - size);
  1304. ASSERT_LE(exp_node->base, new_rgn->base);
  1305. ASSERT_EQ(memblock.reserved.cnt, 1);
  1306. ASSERT_EQ(memblock.reserved.total_size, size);
  1307. test_pass_pop();
  1308. return 0;
  1309. }
  1310. /*
  1311. * A test that tries to allocate a memory region that spans over the min_addr
  1312. * and max_addr range and overlaps with two different nodes, where the requested
  1313. * node ends before min_addr:
  1314. *
  1315. * min_addr
  1316. * | max_addr
  1317. * | |
  1318. * v v
  1319. * | +---------------+ +-------------+---------+ |
  1320. * | | requested | | node1 | node2 | |
  1321. * +----+---------------+--------+-------------+---------+----------+
  1322. * + +
  1323. * | +---------+ |
  1324. * | | rgn | |
  1325. * +----------+---------+-------------------------------------------+
  1326. *
  1327. * Expect to drop the lower limit and allocate a memory region that ends at
  1328. * the end of the requested node.
  1329. */
  1330. static int alloc_nid_top_down_numa_no_overlap_split_check(void)
  1331. {
  1332. int nid_req = 2;
  1333. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1334. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1335. struct memblock_region *node2 = &memblock.memory.regions[6];
  1336. void *allocated_ptr = NULL;
  1337. phys_addr_t size;
  1338. phys_addr_t min_addr;
  1339. phys_addr_t max_addr;
  1340. PREFIX_PUSH();
  1341. setup_numa_memblock(node_fractions);
  1342. size = SZ_512;
  1343. min_addr = node2->base - SZ_256;
  1344. max_addr = min_addr + size;
  1345. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1346. min_addr, max_addr, nid_req);
  1347. ASSERT_NE(allocated_ptr, NULL);
  1348. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1349. ASSERT_EQ(new_rgn->size, size);
  1350. ASSERT_EQ(new_rgn->base, region_end(req_node) - size);
  1351. ASSERT_LE(req_node->base, new_rgn->base);
  1352. ASSERT_EQ(memblock.reserved.cnt, 1);
  1353. ASSERT_EQ(memblock.reserved.total_size, size);
  1354. test_pass_pop();
  1355. return 0;
  1356. }
  1357. /*
  1358. * A test that tries to allocate memory within min_addr and max_add range when
  1359. * the requested node and the range do not overlap, and requested node ends
  1360. * before min_addr. The range overlaps with multiple nodes along node
  1361. * boundaries:
  1362. *
  1363. * min_addr
  1364. * | max_addr
  1365. * | |
  1366. * v v
  1367. * |-----------+ +----------+----...----+----------+ |
  1368. * | requested | | min node | ... | max node | |
  1369. * +-----------+-----------+----------+----...----+----------+------+
  1370. * + +
  1371. * | +-----+ |
  1372. * | | rgn | |
  1373. * +---------------------------------------------------+-----+------+
  1374. *
  1375. * Expect to allocate a memory region at the end of the final node in
  1376. * the range after falling back to NUMA_NO_NODE.
  1377. */
  1378. static int alloc_nid_top_down_numa_no_overlap_low_check(void)
  1379. {
  1380. int nid_req = 0;
  1381. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1382. struct memblock_region *min_node = &memblock.memory.regions[2];
  1383. struct memblock_region *max_node = &memblock.memory.regions[5];
  1384. void *allocated_ptr = NULL;
  1385. phys_addr_t size = SZ_64;
  1386. phys_addr_t max_addr;
  1387. phys_addr_t min_addr;
  1388. PREFIX_PUSH();
  1389. setup_numa_memblock(node_fractions);
  1390. min_addr = min_node->base;
  1391. max_addr = region_end(max_node);
  1392. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1393. min_addr, max_addr, nid_req);
  1394. ASSERT_NE(allocated_ptr, NULL);
  1395. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1396. ASSERT_EQ(new_rgn->size, size);
  1397. ASSERT_EQ(new_rgn->base, max_addr - size);
  1398. ASSERT_LE(max_node->base, new_rgn->base);
  1399. ASSERT_EQ(memblock.reserved.cnt, 1);
  1400. ASSERT_EQ(memblock.reserved.total_size, size);
  1401. test_pass_pop();
  1402. return 0;
  1403. }
  1404. /*
  1405. * A test that tries to allocate memory within min_addr and max_add range when
  1406. * the requested node and the range do not overlap, and requested node starts
  1407. * after max_addr. The range overlaps with multiple nodes along node
  1408. * boundaries:
  1409. *
  1410. * min_addr
  1411. * | max_addr
  1412. * | |
  1413. * v v
  1414. * | +----------+----...----+----------+ +-----------+ |
  1415. * | | min node | ... | max node | | requested | |
  1416. * +-----+----------+----...----+----------+--------+-----------+---+
  1417. * + +
  1418. * | +-----+ |
  1419. * | | rgn | |
  1420. * +---------------------------------+-----+------------------------+
  1421. *
  1422. * Expect to allocate a memory region at the end of the final node in
  1423. * the range after falling back to NUMA_NO_NODE.
  1424. */
  1425. static int alloc_nid_top_down_numa_no_overlap_high_check(void)
  1426. {
  1427. int nid_req = 7;
  1428. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1429. struct memblock_region *min_node = &memblock.memory.regions[2];
  1430. struct memblock_region *max_node = &memblock.memory.regions[5];
  1431. void *allocated_ptr = NULL;
  1432. phys_addr_t size = SZ_64;
  1433. phys_addr_t max_addr;
  1434. phys_addr_t min_addr;
  1435. PREFIX_PUSH();
  1436. setup_numa_memblock(node_fractions);
  1437. min_addr = min_node->base;
  1438. max_addr = region_end(max_node);
  1439. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1440. min_addr, max_addr, nid_req);
  1441. ASSERT_NE(allocated_ptr, NULL);
  1442. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1443. ASSERT_EQ(new_rgn->size, size);
  1444. ASSERT_EQ(new_rgn->base, max_addr - size);
  1445. ASSERT_LE(max_node->base, new_rgn->base);
  1446. ASSERT_EQ(memblock.reserved.cnt, 1);
  1447. ASSERT_EQ(memblock.reserved.total_size, size);
  1448. test_pass_pop();
  1449. return 0;
  1450. }
  1451. /*
  1452. * A test that tries to allocate a memory region in a specific NUMA node that
  1453. * has enough memory to allocate a region of the requested size.
  1454. * Expect to allocate an aligned region at the beginning of the requested node.
  1455. */
  1456. static int alloc_nid_bottom_up_numa_simple_check(void)
  1457. {
  1458. int nid_req = 3;
  1459. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1460. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1461. void *allocated_ptr = NULL;
  1462. phys_addr_t size;
  1463. phys_addr_t min_addr;
  1464. phys_addr_t max_addr;
  1465. PREFIX_PUSH();
  1466. setup_numa_memblock(node_fractions);
  1467. ASSERT_LE(SZ_4, req_node->size);
  1468. size = req_node->size / SZ_4;
  1469. min_addr = memblock_start_of_DRAM();
  1470. max_addr = memblock_end_of_DRAM();
  1471. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1472. min_addr, max_addr, nid_req);
  1473. ASSERT_NE(allocated_ptr, NULL);
  1474. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1475. ASSERT_EQ(new_rgn->size, size);
  1476. ASSERT_EQ(new_rgn->base, req_node->base);
  1477. ASSERT_LE(region_end(new_rgn), region_end(req_node));
  1478. ASSERT_EQ(memblock.reserved.cnt, 1);
  1479. ASSERT_EQ(memblock.reserved.total_size, size);
  1480. test_pass_pop();
  1481. return 0;
  1482. }
  1483. /*
  1484. * A test that tries to allocate a memory region in a specific NUMA node that
  1485. * does not have enough memory to allocate a region of the requested size:
  1486. *
  1487. * |----------------------+-----+ |
  1488. * | expected | req | |
  1489. * +----------------------+-----+----------------+
  1490. *
  1491. * |---------+ |
  1492. * | rgn | |
  1493. * +---------+-----------------------------------+
  1494. *
  1495. * Expect to allocate an aligned region at the beginning of the first node that
  1496. * has enough memory (in this case, nid = 0) after falling back to NUMA_NO_NODE.
  1497. */
  1498. static int alloc_nid_bottom_up_numa_small_node_check(void)
  1499. {
  1500. int nid_req = 1;
  1501. int nid_exp = 0;
  1502. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1503. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1504. struct memblock_region *exp_node = &memblock.memory.regions[nid_exp];
  1505. void *allocated_ptr = NULL;
  1506. phys_addr_t size;
  1507. phys_addr_t min_addr;
  1508. phys_addr_t max_addr;
  1509. PREFIX_PUSH();
  1510. setup_numa_memblock(node_fractions);
  1511. size = SZ_2 * req_node->size;
  1512. min_addr = memblock_start_of_DRAM();
  1513. max_addr = memblock_end_of_DRAM();
  1514. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1515. min_addr, max_addr, nid_req);
  1516. ASSERT_NE(allocated_ptr, NULL);
  1517. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1518. ASSERT_EQ(new_rgn->size, size);
  1519. ASSERT_EQ(new_rgn->base, exp_node->base);
  1520. ASSERT_LE(region_end(new_rgn), region_end(exp_node));
  1521. ASSERT_EQ(memblock.reserved.cnt, 1);
  1522. ASSERT_EQ(memblock.reserved.total_size, size);
  1523. test_pass_pop();
  1524. return 0;
  1525. }
  1526. /*
  1527. * A test that tries to allocate a memory region in a specific NUMA node that
  1528. * is fully reserved:
  1529. *
  1530. * |----------------------+ +-----------+ |
  1531. * | expected | | requested | |
  1532. * +----------------------+-----+-----------+--------------------+
  1533. *
  1534. * |-----------+ +-----------+ |
  1535. * | new | | reserved | |
  1536. * +-----------+----------------+-----------+--------------------+
  1537. *
  1538. * Expect to allocate an aligned region at the beginning of the first node that
  1539. * is large enough and has enough unreserved memory (in this case, nid = 0)
  1540. * after falling back to NUMA_NO_NODE. The region count and total size get
  1541. * updated.
  1542. */
  1543. static int alloc_nid_bottom_up_numa_node_reserved_check(void)
  1544. {
  1545. int nid_req = 2;
  1546. int nid_exp = 0;
  1547. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1548. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1549. struct memblock_region *exp_node = &memblock.memory.regions[nid_exp];
  1550. void *allocated_ptr = NULL;
  1551. phys_addr_t size;
  1552. phys_addr_t min_addr;
  1553. phys_addr_t max_addr;
  1554. PREFIX_PUSH();
  1555. setup_numa_memblock(node_fractions);
  1556. size = req_node->size;
  1557. min_addr = memblock_start_of_DRAM();
  1558. max_addr = memblock_end_of_DRAM();
  1559. memblock_reserve(req_node->base, req_node->size);
  1560. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1561. min_addr, max_addr, nid_req);
  1562. ASSERT_NE(allocated_ptr, NULL);
  1563. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1564. ASSERT_EQ(new_rgn->size, size);
  1565. ASSERT_EQ(new_rgn->base, exp_node->base);
  1566. ASSERT_LE(region_end(new_rgn), region_end(exp_node));
  1567. ASSERT_EQ(memblock.reserved.cnt, 2);
  1568. ASSERT_EQ(memblock.reserved.total_size, size + req_node->size);
  1569. test_pass_pop();
  1570. return 0;
  1571. }
  1572. /*
  1573. * A test that tries to allocate a memory region in a specific NUMA node that
  1574. * is partially reserved but has enough memory for the allocated region:
  1575. *
  1576. * | +---------------------------------------+ |
  1577. * | | requested | |
  1578. * +-----------+---------------------------------------+---------+
  1579. *
  1580. * | +------------------+-----+ |
  1581. * | | reserved | new | |
  1582. * +-----------+------------------+-----+------------------------+
  1583. *
  1584. * Expect to allocate an aligned region in the requested node that merges with
  1585. * the existing reserved region. The total size gets updated.
  1586. */
  1587. static int alloc_nid_bottom_up_numa_part_reserved_check(void)
  1588. {
  1589. int nid_req = 4;
  1590. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1591. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1592. void *allocated_ptr = NULL;
  1593. struct region r1;
  1594. phys_addr_t size;
  1595. phys_addr_t min_addr;
  1596. phys_addr_t max_addr;
  1597. phys_addr_t total_size;
  1598. PREFIX_PUSH();
  1599. setup_numa_memblock(node_fractions);
  1600. ASSERT_LE(SZ_8, req_node->size);
  1601. r1.base = req_node->base;
  1602. r1.size = req_node->size / SZ_2;
  1603. size = r1.size / SZ_4;
  1604. min_addr = memblock_start_of_DRAM();
  1605. max_addr = memblock_end_of_DRAM();
  1606. total_size = size + r1.size;
  1607. memblock_reserve(r1.base, r1.size);
  1608. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1609. min_addr, max_addr, nid_req);
  1610. ASSERT_NE(allocated_ptr, NULL);
  1611. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1612. ASSERT_EQ(new_rgn->size, total_size);
  1613. ASSERT_EQ(new_rgn->base, req_node->base);
  1614. ASSERT_LE(region_end(new_rgn), region_end(req_node));
  1615. ASSERT_EQ(memblock.reserved.cnt, 1);
  1616. ASSERT_EQ(memblock.reserved.total_size, total_size);
  1617. test_pass_pop();
  1618. return 0;
  1619. }
  1620. /*
  1621. * A test that tries to allocate a memory region in a specific NUMA node that
  1622. * is partially reserved and does not have enough contiguous memory for the
  1623. * allocated region:
  1624. *
  1625. * |----------------------+ +-----------------------+ |
  1626. * | expected | | requested | |
  1627. * +----------------------+-------+-----------------------+---------+
  1628. *
  1629. * |-----------+ +----------+ |
  1630. * | new | | reserved | |
  1631. * +-----------+------------------------+----------+----------------+
  1632. *
  1633. * Expect to allocate an aligned region at the beginning of the first
  1634. * node that is large enough and has enough unreserved memory (in this case,
  1635. * nid = 0) after falling back to NUMA_NO_NODE. The region count and total size
  1636. * get updated.
  1637. */
  1638. static int alloc_nid_bottom_up_numa_part_reserved_fallback_check(void)
  1639. {
  1640. int nid_req = 4;
  1641. int nid_exp = 0;
  1642. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1643. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1644. struct memblock_region *exp_node = &memblock.memory.regions[nid_exp];
  1645. void *allocated_ptr = NULL;
  1646. struct region r1;
  1647. phys_addr_t size;
  1648. phys_addr_t min_addr;
  1649. phys_addr_t max_addr;
  1650. PREFIX_PUSH();
  1651. setup_numa_memblock(node_fractions);
  1652. ASSERT_LE(SZ_4, req_node->size);
  1653. size = req_node->size / SZ_2;
  1654. r1.base = req_node->base + (size / SZ_2);
  1655. r1.size = size;
  1656. min_addr = memblock_start_of_DRAM();
  1657. max_addr = memblock_end_of_DRAM();
  1658. memblock_reserve(r1.base, r1.size);
  1659. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1660. min_addr, max_addr, nid_req);
  1661. ASSERT_NE(allocated_ptr, NULL);
  1662. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1663. ASSERT_EQ(new_rgn->size, size);
  1664. ASSERT_EQ(new_rgn->base, exp_node->base);
  1665. ASSERT_LE(region_end(new_rgn), region_end(exp_node));
  1666. ASSERT_EQ(memblock.reserved.cnt, 2);
  1667. ASSERT_EQ(memblock.reserved.total_size, size + r1.size);
  1668. test_pass_pop();
  1669. return 0;
  1670. }
  1671. /*
  1672. * A test that tries to allocate a memory region that spans over the min_addr
  1673. * and max_addr range and overlaps with two different nodes, where the first
  1674. * node is the requested node:
  1675. *
  1676. * min_addr
  1677. * | max_addr
  1678. * | |
  1679. * v v
  1680. * | +-----------------------+-----------+ |
  1681. * | | requested | node3 | |
  1682. * +-----------+-----------------------+-----------+--------------+
  1683. * + +
  1684. * | +-----------+ |
  1685. * | | rgn | |
  1686. * +-----------+-----------+--------------------------------------+
  1687. *
  1688. * Expect to drop the lower limit and allocate a memory region at the beginning
  1689. * of the requested node.
  1690. */
  1691. static int alloc_nid_bottom_up_numa_split_range_low_check(void)
  1692. {
  1693. int nid_req = 2;
  1694. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1695. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1696. void *allocated_ptr = NULL;
  1697. phys_addr_t size = SZ_512;
  1698. phys_addr_t min_addr;
  1699. phys_addr_t max_addr;
  1700. phys_addr_t req_node_end;
  1701. PREFIX_PUSH();
  1702. setup_numa_memblock(node_fractions);
  1703. req_node_end = region_end(req_node);
  1704. min_addr = req_node_end - SZ_256;
  1705. max_addr = min_addr + size;
  1706. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1707. min_addr, max_addr, nid_req);
  1708. ASSERT_NE(allocated_ptr, NULL);
  1709. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1710. ASSERT_EQ(new_rgn->size, size);
  1711. ASSERT_EQ(new_rgn->base, req_node->base);
  1712. ASSERT_LE(region_end(new_rgn), req_node_end);
  1713. ASSERT_EQ(memblock.reserved.cnt, 1);
  1714. ASSERT_EQ(memblock.reserved.total_size, size);
  1715. test_pass_pop();
  1716. return 0;
  1717. }
  1718. /*
  1719. * A test that tries to allocate a memory region that spans over the min_addr
  1720. * and max_addr range and overlaps with two different nodes, where the second
  1721. * node is the requested node:
  1722. *
  1723. * min_addr
  1724. * | max_addr
  1725. * | |
  1726. * v v
  1727. * |------------------+ +----------------------+---------+ |
  1728. * | expected | | previous |requested| |
  1729. * +------------------+--------+----------------------+---------+------+
  1730. * + +
  1731. * |---------+ |
  1732. * | rgn | |
  1733. * +---------+---------------------------------------------------------+
  1734. *
  1735. * Expect to drop the lower limit and allocate a memory region at the beginning
  1736. * of the first node that has enough memory.
  1737. */
  1738. static int alloc_nid_bottom_up_numa_split_range_high_check(void)
  1739. {
  1740. int nid_req = 3;
  1741. int nid_exp = 0;
  1742. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1743. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1744. struct memblock_region *exp_node = &memblock.memory.regions[nid_exp];
  1745. void *allocated_ptr = NULL;
  1746. phys_addr_t size = SZ_512;
  1747. phys_addr_t min_addr;
  1748. phys_addr_t max_addr;
  1749. phys_addr_t exp_node_end;
  1750. PREFIX_PUSH();
  1751. setup_numa_memblock(node_fractions);
  1752. exp_node_end = region_end(req_node);
  1753. min_addr = req_node->base - SZ_256;
  1754. max_addr = min_addr + size;
  1755. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1756. min_addr, max_addr, nid_req);
  1757. ASSERT_NE(allocated_ptr, NULL);
  1758. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1759. ASSERT_EQ(new_rgn->size, size);
  1760. ASSERT_EQ(new_rgn->base, exp_node->base);
  1761. ASSERT_LE(region_end(new_rgn), exp_node_end);
  1762. ASSERT_EQ(memblock.reserved.cnt, 1);
  1763. ASSERT_EQ(memblock.reserved.total_size, size);
  1764. test_pass_pop();
  1765. return 0;
  1766. }
  1767. /*
  1768. * A test that tries to allocate a memory region that spans over the min_addr
  1769. * and max_addr range and overlaps with two different nodes, where the requested
  1770. * node ends before min_addr:
  1771. *
  1772. * min_addr
  1773. * | max_addr
  1774. * | |
  1775. * v v
  1776. * | +---------------+ +-------------+---------+ |
  1777. * | | requested | | node1 | node2 | |
  1778. * +----+---------------+--------+-------------+---------+---------+
  1779. * + +
  1780. * | +---------+ |
  1781. * | | rgn | |
  1782. * +----+---------+------------------------------------------------+
  1783. *
  1784. * Expect to drop the lower limit and allocate a memory region that starts at
  1785. * the beginning of the requested node.
  1786. */
  1787. static int alloc_nid_bottom_up_numa_no_overlap_split_check(void)
  1788. {
  1789. int nid_req = 2;
  1790. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1791. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1792. struct memblock_region *node2 = &memblock.memory.regions[6];
  1793. void *allocated_ptr = NULL;
  1794. phys_addr_t size;
  1795. phys_addr_t min_addr;
  1796. phys_addr_t max_addr;
  1797. PREFIX_PUSH();
  1798. setup_numa_memblock(node_fractions);
  1799. size = SZ_512;
  1800. min_addr = node2->base - SZ_256;
  1801. max_addr = min_addr + size;
  1802. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1803. min_addr, max_addr, nid_req);
  1804. ASSERT_NE(allocated_ptr, NULL);
  1805. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1806. ASSERT_EQ(new_rgn->size, size);
  1807. ASSERT_EQ(new_rgn->base, req_node->base);
  1808. ASSERT_LE(region_end(new_rgn), region_end(req_node));
  1809. ASSERT_EQ(memblock.reserved.cnt, 1);
  1810. ASSERT_EQ(memblock.reserved.total_size, size);
  1811. test_pass_pop();
  1812. return 0;
  1813. }
  1814. /*
  1815. * A test that tries to allocate memory within min_addr and max_add range when
  1816. * the requested node and the range do not overlap, and requested node ends
  1817. * before min_addr. The range overlaps with multiple nodes along node
  1818. * boundaries:
  1819. *
  1820. * min_addr
  1821. * | max_addr
  1822. * | |
  1823. * v v
  1824. * |-----------+ +----------+----...----+----------+ |
  1825. * | requested | | min node | ... | max node | |
  1826. * +-----------+-----------+----------+----...----+----------+------+
  1827. * + +
  1828. * | +-----+ |
  1829. * | | rgn | |
  1830. * +-----------------------+-----+----------------------------------+
  1831. *
  1832. * Expect to allocate a memory region at the beginning of the first node
  1833. * in the range after falling back to NUMA_NO_NODE.
  1834. */
  1835. static int alloc_nid_bottom_up_numa_no_overlap_low_check(void)
  1836. {
  1837. int nid_req = 0;
  1838. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1839. struct memblock_region *min_node = &memblock.memory.regions[2];
  1840. struct memblock_region *max_node = &memblock.memory.regions[5];
  1841. void *allocated_ptr = NULL;
  1842. phys_addr_t size = SZ_64;
  1843. phys_addr_t max_addr;
  1844. phys_addr_t min_addr;
  1845. PREFIX_PUSH();
  1846. setup_numa_memblock(node_fractions);
  1847. min_addr = min_node->base;
  1848. max_addr = region_end(max_node);
  1849. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1850. min_addr, max_addr, nid_req);
  1851. ASSERT_NE(allocated_ptr, NULL);
  1852. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1853. ASSERT_EQ(new_rgn->size, size);
  1854. ASSERT_EQ(new_rgn->base, min_addr);
  1855. ASSERT_LE(region_end(new_rgn), region_end(min_node));
  1856. ASSERT_EQ(memblock.reserved.cnt, 1);
  1857. ASSERT_EQ(memblock.reserved.total_size, size);
  1858. test_pass_pop();
  1859. return 0;
  1860. }
  1861. /*
  1862. * A test that tries to allocate memory within min_addr and max_add range when
  1863. * the requested node and the range do not overlap, and requested node starts
  1864. * after max_addr. The range overlaps with multiple nodes along node
  1865. * boundaries:
  1866. *
  1867. * min_addr
  1868. * | max_addr
  1869. * | |
  1870. * v v
  1871. * | +----------+----...----+----------+ +---------+ |
  1872. * | | min node | ... | max node | |requested| |
  1873. * +-----+----------+----...----+----------+---------+---------+---+
  1874. * + +
  1875. * | +-----+ |
  1876. * | | rgn | |
  1877. * +-----+-----+---------------------------------------------------+
  1878. *
  1879. * Expect to allocate a memory region at the beginning of the first node
  1880. * in the range after falling back to NUMA_NO_NODE.
  1881. */
  1882. static int alloc_nid_bottom_up_numa_no_overlap_high_check(void)
  1883. {
  1884. int nid_req = 7;
  1885. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1886. struct memblock_region *min_node = &memblock.memory.regions[2];
  1887. struct memblock_region *max_node = &memblock.memory.regions[5];
  1888. void *allocated_ptr = NULL;
  1889. phys_addr_t size = SZ_64;
  1890. phys_addr_t max_addr;
  1891. phys_addr_t min_addr;
  1892. PREFIX_PUSH();
  1893. setup_numa_memblock(node_fractions);
  1894. min_addr = min_node->base;
  1895. max_addr = region_end(max_node);
  1896. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1897. min_addr, max_addr, nid_req);
  1898. ASSERT_NE(allocated_ptr, NULL);
  1899. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1900. ASSERT_EQ(new_rgn->size, size);
  1901. ASSERT_EQ(new_rgn->base, min_addr);
  1902. ASSERT_LE(region_end(new_rgn), region_end(min_node));
  1903. ASSERT_EQ(memblock.reserved.cnt, 1);
  1904. ASSERT_EQ(memblock.reserved.total_size, size);
  1905. test_pass_pop();
  1906. return 0;
  1907. }
  1908. /*
  1909. * A test that tries to allocate a memory region in a specific NUMA node that
  1910. * does not have enough memory to allocate a region of the requested size.
  1911. * Additionally, none of the nodes have enough memory to allocate the region:
  1912. *
  1913. * +-----------------------------------+
  1914. * | new |
  1915. * +-----------------------------------+
  1916. * |-------+-------+-------+-------+-------+-------+-------+-------|
  1917. * | node0 | node1 | node2 | node3 | node4 | node5 | node6 | node7 |
  1918. * +-------+-------+-------+-------+-------+-------+-------+-------+
  1919. *
  1920. * Expect no allocation to happen.
  1921. */
  1922. static int alloc_nid_numa_large_region_generic_check(void)
  1923. {
  1924. int nid_req = 3;
  1925. void *allocated_ptr = NULL;
  1926. phys_addr_t size = MEM_SIZE / SZ_2;
  1927. phys_addr_t min_addr;
  1928. phys_addr_t max_addr;
  1929. PREFIX_PUSH();
  1930. setup_numa_memblock(node_fractions);
  1931. min_addr = memblock_start_of_DRAM();
  1932. max_addr = memblock_end_of_DRAM();
  1933. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1934. min_addr, max_addr, nid_req);
  1935. ASSERT_EQ(allocated_ptr, NULL);
  1936. test_pass_pop();
  1937. return 0;
  1938. }
  1939. /*
  1940. * A test that tries to allocate memory within min_addr and max_addr range when
  1941. * there are two reserved regions at the borders. The requested node starts at
  1942. * min_addr and ends at max_addr and is the same size as the region to be
  1943. * allocated:
  1944. *
  1945. * min_addr
  1946. * | max_addr
  1947. * | |
  1948. * v v
  1949. * | +-----------+-----------------------+-----------------------|
  1950. * | | node5 | requested | node7 |
  1951. * +------+-----------+-----------------------+-----------------------+
  1952. * + +
  1953. * | +----+-----------------------+----+ |
  1954. * | | r2 | new | r1 | |
  1955. * +-------------+----+-----------------------+----+------------------+
  1956. *
  1957. * Expect to merge all of the regions into one. The region counter and total
  1958. * size fields get updated.
  1959. */
  1960. static int alloc_nid_numa_reserved_full_merge_generic_check(void)
  1961. {
  1962. int nid_req = 6;
  1963. int nid_next = nid_req + 1;
  1964. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  1965. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  1966. struct memblock_region *next_node = &memblock.memory.regions[nid_next];
  1967. void *allocated_ptr = NULL;
  1968. struct region r1, r2;
  1969. phys_addr_t size = req_node->size;
  1970. phys_addr_t total_size;
  1971. phys_addr_t max_addr;
  1972. phys_addr_t min_addr;
  1973. PREFIX_PUSH();
  1974. setup_numa_memblock(node_fractions);
  1975. r1.base = next_node->base;
  1976. r1.size = SZ_128;
  1977. r2.size = SZ_128;
  1978. r2.base = r1.base - (size + r2.size);
  1979. total_size = r1.size + r2.size + size;
  1980. min_addr = r2.base + r2.size;
  1981. max_addr = r1.base;
  1982. memblock_reserve(r1.base, r1.size);
  1983. memblock_reserve(r2.base, r2.size);
  1984. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  1985. min_addr, max_addr, nid_req);
  1986. ASSERT_NE(allocated_ptr, NULL);
  1987. assert_mem_content(allocated_ptr, size, alloc_nid_test_flags);
  1988. ASSERT_EQ(new_rgn->size, total_size);
  1989. ASSERT_EQ(new_rgn->base, r2.base);
  1990. ASSERT_LE(new_rgn->base, req_node->base);
  1991. ASSERT_LE(region_end(req_node), region_end(new_rgn));
  1992. ASSERT_EQ(memblock.reserved.cnt, 1);
  1993. ASSERT_EQ(memblock.reserved.total_size, total_size);
  1994. test_pass_pop();
  1995. return 0;
  1996. }
  1997. /*
  1998. * A test that tries to allocate memory within min_addr and max_add range,
  1999. * where the total range can fit the region, but it is split between two nodes
  2000. * and everything else is reserved. Additionally, nid is set to NUMA_NO_NODE
  2001. * instead of requesting a specific node:
  2002. *
  2003. * +-----------+
  2004. * | new |
  2005. * +-----------+
  2006. * | +---------------------+-----------|
  2007. * | | prev node | next node |
  2008. * +------+---------------------+-----------+
  2009. * + +
  2010. * |----------------------+ +-----|
  2011. * | r1 | | r2 |
  2012. * +----------------------+-----------+-----+
  2013. * ^ ^
  2014. * | |
  2015. * | max_addr
  2016. * |
  2017. * min_addr
  2018. *
  2019. * Expect no allocation to happen.
  2020. */
  2021. static int alloc_nid_numa_split_all_reserved_generic_check(void)
  2022. {
  2023. void *allocated_ptr = NULL;
  2024. struct memblock_region *next_node = &memblock.memory.regions[7];
  2025. struct region r1, r2;
  2026. phys_addr_t size = SZ_256;
  2027. phys_addr_t max_addr;
  2028. phys_addr_t min_addr;
  2029. PREFIX_PUSH();
  2030. setup_numa_memblock(node_fractions);
  2031. r2.base = next_node->base + SZ_128;
  2032. r2.size = memblock_end_of_DRAM() - r2.base;
  2033. r1.size = MEM_SIZE - (r2.size + size);
  2034. r1.base = memblock_start_of_DRAM();
  2035. min_addr = r1.base + r1.size;
  2036. max_addr = r2.base;
  2037. memblock_reserve(r1.base, r1.size);
  2038. memblock_reserve(r2.base, r2.size);
  2039. allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,
  2040. min_addr, max_addr,
  2041. NUMA_NO_NODE);
  2042. ASSERT_EQ(allocated_ptr, NULL);
  2043. test_pass_pop();
  2044. return 0;
  2045. }
  2046. /*
  2047. * A simple test that tries to allocate a memory region through the
  2048. * memblock_alloc_node() on a NUMA node with id `nid`. Expected to have the
  2049. * correct NUMA node set for the new region.
  2050. */
  2051. static int alloc_node_on_correct_nid(void)
  2052. {
  2053. int nid_req = 2;
  2054. void *allocated_ptr = NULL;
  2055. #ifdef CONFIG_NUMA
  2056. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  2057. #endif
  2058. phys_addr_t size = SZ_512;
  2059. PREFIX_PUSH();
  2060. setup_numa_memblock(node_fractions);
  2061. allocated_ptr = memblock_alloc_node(size, SMP_CACHE_BYTES, nid_req);
  2062. ASSERT_NE(allocated_ptr, NULL);
  2063. #ifdef CONFIG_NUMA
  2064. ASSERT_EQ(nid_req, req_node->nid);
  2065. #endif
  2066. test_pass_pop();
  2067. return 0;
  2068. }
  2069. /* Test case wrappers for NUMA tests */
  2070. static int alloc_nid_numa_simple_check(void)
  2071. {
  2072. test_print("\tRunning %s...\n", __func__);
  2073. memblock_set_bottom_up(false);
  2074. alloc_nid_top_down_numa_simple_check();
  2075. memblock_set_bottom_up(true);
  2076. alloc_nid_bottom_up_numa_simple_check();
  2077. return 0;
  2078. }
  2079. static int alloc_nid_numa_small_node_check(void)
  2080. {
  2081. test_print("\tRunning %s...\n", __func__);
  2082. memblock_set_bottom_up(false);
  2083. alloc_nid_top_down_numa_small_node_check();
  2084. memblock_set_bottom_up(true);
  2085. alloc_nid_bottom_up_numa_small_node_check();
  2086. return 0;
  2087. }
  2088. static int alloc_nid_numa_node_reserved_check(void)
  2089. {
  2090. test_print("\tRunning %s...\n", __func__);
  2091. memblock_set_bottom_up(false);
  2092. alloc_nid_top_down_numa_node_reserved_check();
  2093. memblock_set_bottom_up(true);
  2094. alloc_nid_bottom_up_numa_node_reserved_check();
  2095. return 0;
  2096. }
  2097. static int alloc_nid_numa_part_reserved_check(void)
  2098. {
  2099. test_print("\tRunning %s...\n", __func__);
  2100. memblock_set_bottom_up(false);
  2101. alloc_nid_top_down_numa_part_reserved_check();
  2102. memblock_set_bottom_up(true);
  2103. alloc_nid_bottom_up_numa_part_reserved_check();
  2104. return 0;
  2105. }
  2106. static int alloc_nid_numa_part_reserved_fallback_check(void)
  2107. {
  2108. test_print("\tRunning %s...\n", __func__);
  2109. memblock_set_bottom_up(false);
  2110. alloc_nid_top_down_numa_part_reserved_fallback_check();
  2111. memblock_set_bottom_up(true);
  2112. alloc_nid_bottom_up_numa_part_reserved_fallback_check();
  2113. return 0;
  2114. }
  2115. static int alloc_nid_numa_split_range_low_check(void)
  2116. {
  2117. test_print("\tRunning %s...\n", __func__);
  2118. memblock_set_bottom_up(false);
  2119. alloc_nid_top_down_numa_split_range_low_check();
  2120. memblock_set_bottom_up(true);
  2121. alloc_nid_bottom_up_numa_split_range_low_check();
  2122. return 0;
  2123. }
  2124. static int alloc_nid_numa_split_range_high_check(void)
  2125. {
  2126. test_print("\tRunning %s...\n", __func__);
  2127. memblock_set_bottom_up(false);
  2128. alloc_nid_top_down_numa_split_range_high_check();
  2129. memblock_set_bottom_up(true);
  2130. alloc_nid_bottom_up_numa_split_range_high_check();
  2131. return 0;
  2132. }
  2133. static int alloc_nid_numa_no_overlap_split_check(void)
  2134. {
  2135. test_print("\tRunning %s...\n", __func__);
  2136. memblock_set_bottom_up(false);
  2137. alloc_nid_top_down_numa_no_overlap_split_check();
  2138. memblock_set_bottom_up(true);
  2139. alloc_nid_bottom_up_numa_no_overlap_split_check();
  2140. return 0;
  2141. }
  2142. static int alloc_nid_numa_no_overlap_low_check(void)
  2143. {
  2144. test_print("\tRunning %s...\n", __func__);
  2145. memblock_set_bottom_up(false);
  2146. alloc_nid_top_down_numa_no_overlap_low_check();
  2147. memblock_set_bottom_up(true);
  2148. alloc_nid_bottom_up_numa_no_overlap_low_check();
  2149. return 0;
  2150. }
  2151. static int alloc_nid_numa_no_overlap_high_check(void)
  2152. {
  2153. test_print("\tRunning %s...\n", __func__);
  2154. memblock_set_bottom_up(false);
  2155. alloc_nid_top_down_numa_no_overlap_high_check();
  2156. memblock_set_bottom_up(true);
  2157. alloc_nid_bottom_up_numa_no_overlap_high_check();
  2158. return 0;
  2159. }
  2160. static int alloc_nid_numa_large_region_check(void)
  2161. {
  2162. test_print("\tRunning %s...\n", __func__);
  2163. run_top_down(alloc_nid_numa_large_region_generic_check);
  2164. run_bottom_up(alloc_nid_numa_large_region_generic_check);
  2165. return 0;
  2166. }
  2167. static int alloc_nid_numa_reserved_full_merge_check(void)
  2168. {
  2169. test_print("\tRunning %s...\n", __func__);
  2170. run_top_down(alloc_nid_numa_reserved_full_merge_generic_check);
  2171. run_bottom_up(alloc_nid_numa_reserved_full_merge_generic_check);
  2172. return 0;
  2173. }
  2174. static int alloc_nid_numa_split_all_reserved_check(void)
  2175. {
  2176. test_print("\tRunning %s...\n", __func__);
  2177. run_top_down(alloc_nid_numa_split_all_reserved_generic_check);
  2178. run_bottom_up(alloc_nid_numa_split_all_reserved_generic_check);
  2179. return 0;
  2180. }
  2181. static int alloc_node_numa_on_correct_nid(void)
  2182. {
  2183. test_print("\tRunning %s...\n", __func__);
  2184. run_top_down(alloc_node_on_correct_nid);
  2185. run_bottom_up(alloc_node_on_correct_nid);
  2186. return 0;
  2187. }
  2188. int __memblock_alloc_nid_numa_checks(void)
  2189. {
  2190. test_print("Running %s NUMA tests...\n",
  2191. get_memblock_alloc_nid_name(alloc_nid_test_flags));
  2192. alloc_nid_numa_simple_check();
  2193. alloc_nid_numa_small_node_check();
  2194. alloc_nid_numa_node_reserved_check();
  2195. alloc_nid_numa_part_reserved_check();
  2196. alloc_nid_numa_part_reserved_fallback_check();
  2197. alloc_nid_numa_split_range_low_check();
  2198. alloc_nid_numa_split_range_high_check();
  2199. alloc_nid_numa_no_overlap_split_check();
  2200. alloc_nid_numa_no_overlap_low_check();
  2201. alloc_nid_numa_no_overlap_high_check();
  2202. alloc_nid_numa_large_region_check();
  2203. alloc_nid_numa_reserved_full_merge_check();
  2204. alloc_nid_numa_split_all_reserved_check();
  2205. alloc_node_numa_on_correct_nid();
  2206. return 0;
  2207. }
  2208. static int memblock_alloc_nid_checks_internal(int flags)
  2209. {
  2210. alloc_nid_test_flags = flags;
  2211. prefix_reset();
  2212. prefix_push(get_memblock_alloc_nid_name(flags));
  2213. reset_memblock_attributes();
  2214. dummy_physical_memory_init();
  2215. memblock_alloc_nid_range_checks();
  2216. memblock_alloc_nid_numa_checks();
  2217. dummy_physical_memory_cleanup();
  2218. prefix_pop();
  2219. return 0;
  2220. }
  2221. int memblock_alloc_nid_checks(void)
  2222. {
  2223. memblock_alloc_nid_checks_internal(TEST_F_NONE);
  2224. memblock_alloc_nid_checks_internal(TEST_F_RAW);
  2225. return 0;
  2226. }
  2227. int memblock_alloc_exact_nid_range_checks(void)
  2228. {
  2229. alloc_nid_test_flags = (TEST_F_RAW | TEST_F_EXACT);
  2230. memblock_alloc_nid_range_checks();
  2231. return 0;
  2232. }