huge_memory.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132
  1. /*
  2. * Copyright (C) 2009 Red Hat, Inc.
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2. See
  5. * the COPYING file in the top-level directory.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/mm.h>
  9. #include <linux/sched.h>
  10. #include <linux/highmem.h>
  11. #include <linux/hugetlb.h>
  12. #include <linux/mmu_notifier.h>
  13. #include <linux/rmap.h>
  14. #include <linux/swap.h>
  15. #include <linux/shrinker.h>
  16. #include <linux/mm_inline.h>
  17. #include <linux/dax.h>
  18. #include <linux/kthread.h>
  19. #include <linux/khugepaged.h>
  20. #include <linux/freezer.h>
  21. #include <linux/mman.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/migrate.h>
  24. #include <linux/hashtable.h>
  25. #include <linux/userfaultfd_k.h>
  26. #include <linux/page_idle.h>
  27. #include <asm/tlb.h>
  28. #include <asm/pgalloc.h>
  29. #include "internal.h"
  30. /*
  31. * By default transparent hugepage support is disabled in order that avoid
  32. * to risk increase the memory footprint of applications without a guaranteed
  33. * benefit. When transparent hugepage support is enabled, is for all mappings,
  34. * and khugepaged scans all mappings.
  35. * Defrag is invoked by khugepaged hugepage allocations and by page faults
  36. * for all hugepage allocations.
  37. */
  38. unsigned long transparent_hugepage_flags __read_mostly =
  39. #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
  40. (1<<TRANSPARENT_HUGEPAGE_FLAG)|
  41. #endif
  42. #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
  43. (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
  44. #endif
  45. (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
  46. (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
  47. (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
  48. /* default scan 8*512 pte (or vmas) every 30 second */
  49. static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
  50. static unsigned int khugepaged_pages_collapsed;
  51. static unsigned int khugepaged_full_scans;
  52. static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
  53. /* during fragmentation poll the hugepage allocator once every minute */
  54. static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
  55. static struct task_struct *khugepaged_thread __read_mostly;
  56. static DEFINE_MUTEX(khugepaged_mutex);
  57. static DEFINE_SPINLOCK(khugepaged_mm_lock);
  58. static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
  59. /*
  60. * default collapse hugepages if there is at least one pte mapped like
  61. * it would have happened if the vma was large enough during page
  62. * fault.
  63. */
  64. static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
  65. static int khugepaged(void *none);
  66. static int khugepaged_slab_init(void);
  67. static void khugepaged_slab_exit(void);
  68. #define MM_SLOTS_HASH_BITS 10
  69. static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
  70. static struct kmem_cache *mm_slot_cache __read_mostly;
  71. /**
  72. * struct mm_slot - hash lookup from mm to mm_slot
  73. * @hash: hash collision list
  74. * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
  75. * @mm: the mm that this information is valid for
  76. */
  77. struct mm_slot {
  78. struct hlist_node hash;
  79. struct list_head mm_node;
  80. struct mm_struct *mm;
  81. };
  82. /**
  83. * struct khugepaged_scan - cursor for scanning
  84. * @mm_head: the head of the mm list to scan
  85. * @mm_slot: the current mm_slot we are scanning
  86. * @address: the next address inside that to be scanned
  87. *
  88. * There is only the one khugepaged_scan instance of this cursor structure.
  89. */
  90. struct khugepaged_scan {
  91. struct list_head mm_head;
  92. struct mm_slot *mm_slot;
  93. unsigned long address;
  94. };
  95. static struct khugepaged_scan khugepaged_scan = {
  96. .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
  97. };
  98. static void set_recommended_min_free_kbytes(void)
  99. {
  100. struct zone *zone;
  101. int nr_zones = 0;
  102. unsigned long recommended_min;
  103. for_each_populated_zone(zone)
  104. nr_zones++;
  105. /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
  106. recommended_min = pageblock_nr_pages * nr_zones * 2;
  107. /*
  108. * Make sure that on average at least two pageblocks are almost free
  109. * of another type, one for a migratetype to fall back to and a
  110. * second to avoid subsequent fallbacks of other types There are 3
  111. * MIGRATE_TYPES we care about.
  112. */
  113. recommended_min += pageblock_nr_pages * nr_zones *
  114. MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
  115. /* don't ever allow to reserve more than 5% of the lowmem */
  116. recommended_min = min(recommended_min,
  117. (unsigned long) nr_free_buffer_pages() / 20);
  118. recommended_min <<= (PAGE_SHIFT-10);
  119. if (recommended_min > min_free_kbytes) {
  120. if (user_min_free_kbytes >= 0)
  121. pr_info("raising min_free_kbytes from %d to %lu "
  122. "to help transparent hugepage allocations\n",
  123. min_free_kbytes, recommended_min);
  124. min_free_kbytes = recommended_min;
  125. }
  126. setup_per_zone_wmarks();
  127. }
  128. static int start_stop_khugepaged(void)
  129. {
  130. int err = 0;
  131. if (khugepaged_enabled()) {
  132. if (!khugepaged_thread)
  133. khugepaged_thread = kthread_run(khugepaged, NULL,
  134. "khugepaged");
  135. if (IS_ERR(khugepaged_thread)) {
  136. pr_err("khugepaged: kthread_run(khugepaged) failed\n");
  137. err = PTR_ERR(khugepaged_thread);
  138. khugepaged_thread = NULL;
  139. goto fail;
  140. }
  141. if (!list_empty(&khugepaged_scan.mm_head))
  142. wake_up_interruptible(&khugepaged_wait);
  143. set_recommended_min_free_kbytes();
  144. } else if (khugepaged_thread) {
  145. kthread_stop(khugepaged_thread);
  146. khugepaged_thread = NULL;
  147. }
  148. fail:
  149. return err;
  150. }
  151. static atomic_t huge_zero_refcount;
  152. struct page *huge_zero_page __read_mostly;
  153. struct page *get_huge_zero_page(void)
  154. {
  155. struct page *zero_page;
  156. retry:
  157. if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
  158. return READ_ONCE(huge_zero_page);
  159. zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
  160. HPAGE_PMD_ORDER);
  161. if (!zero_page) {
  162. count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
  163. return NULL;
  164. }
  165. count_vm_event(THP_ZERO_PAGE_ALLOC);
  166. preempt_disable();
  167. if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
  168. preempt_enable();
  169. __free_pages(zero_page, compound_order(zero_page));
  170. goto retry;
  171. }
  172. /* We take additional reference here. It will be put back by shrinker */
  173. atomic_set(&huge_zero_refcount, 2);
  174. preempt_enable();
  175. return READ_ONCE(huge_zero_page);
  176. }
  177. static void put_huge_zero_page(void)
  178. {
  179. /*
  180. * Counter should never go to zero here. Only shrinker can put
  181. * last reference.
  182. */
  183. BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
  184. }
  185. static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
  186. struct shrink_control *sc)
  187. {
  188. /* we can free zero page only if last reference remains */
  189. return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
  190. }
  191. static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
  192. struct shrink_control *sc)
  193. {
  194. if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
  195. struct page *zero_page = xchg(&huge_zero_page, NULL);
  196. BUG_ON(zero_page == NULL);
  197. __free_pages(zero_page, compound_order(zero_page));
  198. return HPAGE_PMD_NR;
  199. }
  200. return 0;
  201. }
  202. static struct shrinker huge_zero_page_shrinker = {
  203. .count_objects = shrink_huge_zero_page_count,
  204. .scan_objects = shrink_huge_zero_page_scan,
  205. .seeks = DEFAULT_SEEKS,
  206. };
  207. #ifdef CONFIG_SYSFS
  208. static ssize_t double_flag_show(struct kobject *kobj,
  209. struct kobj_attribute *attr, char *buf,
  210. enum transparent_hugepage_flag enabled,
  211. enum transparent_hugepage_flag req_madv)
  212. {
  213. if (test_bit(enabled, &transparent_hugepage_flags)) {
  214. VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
  215. return sprintf(buf, "[always] madvise never\n");
  216. } else if (test_bit(req_madv, &transparent_hugepage_flags))
  217. return sprintf(buf, "always [madvise] never\n");
  218. else
  219. return sprintf(buf, "always madvise [never]\n");
  220. }
  221. static ssize_t double_flag_store(struct kobject *kobj,
  222. struct kobj_attribute *attr,
  223. const char *buf, size_t count,
  224. enum transparent_hugepage_flag enabled,
  225. enum transparent_hugepage_flag req_madv)
  226. {
  227. if (!memcmp("always", buf,
  228. min(sizeof("always")-1, count))) {
  229. set_bit(enabled, &transparent_hugepage_flags);
  230. clear_bit(req_madv, &transparent_hugepage_flags);
  231. } else if (!memcmp("madvise", buf,
  232. min(sizeof("madvise")-1, count))) {
  233. clear_bit(enabled, &transparent_hugepage_flags);
  234. set_bit(req_madv, &transparent_hugepage_flags);
  235. } else if (!memcmp("never", buf,
  236. min(sizeof("never")-1, count))) {
  237. clear_bit(enabled, &transparent_hugepage_flags);
  238. clear_bit(req_madv, &transparent_hugepage_flags);
  239. } else
  240. return -EINVAL;
  241. return count;
  242. }
  243. static ssize_t enabled_show(struct kobject *kobj,
  244. struct kobj_attribute *attr, char *buf)
  245. {
  246. return double_flag_show(kobj, attr, buf,
  247. TRANSPARENT_HUGEPAGE_FLAG,
  248. TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
  249. }
  250. static ssize_t enabled_store(struct kobject *kobj,
  251. struct kobj_attribute *attr,
  252. const char *buf, size_t count)
  253. {
  254. ssize_t ret;
  255. ret = double_flag_store(kobj, attr, buf, count,
  256. TRANSPARENT_HUGEPAGE_FLAG,
  257. TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
  258. if (ret > 0) {
  259. int err;
  260. mutex_lock(&khugepaged_mutex);
  261. err = start_stop_khugepaged();
  262. mutex_unlock(&khugepaged_mutex);
  263. if (err)
  264. ret = err;
  265. }
  266. return ret;
  267. }
  268. static struct kobj_attribute enabled_attr =
  269. __ATTR(enabled, 0644, enabled_show, enabled_store);
  270. static ssize_t single_flag_show(struct kobject *kobj,
  271. struct kobj_attribute *attr, char *buf,
  272. enum transparent_hugepage_flag flag)
  273. {
  274. return sprintf(buf, "%d\n",
  275. !!test_bit(flag, &transparent_hugepage_flags));
  276. }
  277. static ssize_t single_flag_store(struct kobject *kobj,
  278. struct kobj_attribute *attr,
  279. const char *buf, size_t count,
  280. enum transparent_hugepage_flag flag)
  281. {
  282. unsigned long value;
  283. int ret;
  284. ret = kstrtoul(buf, 10, &value);
  285. if (ret < 0)
  286. return ret;
  287. if (value > 1)
  288. return -EINVAL;
  289. if (value)
  290. set_bit(flag, &transparent_hugepage_flags);
  291. else
  292. clear_bit(flag, &transparent_hugepage_flags);
  293. return count;
  294. }
  295. /*
  296. * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
  297. * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
  298. * memory just to allocate one more hugepage.
  299. */
  300. static ssize_t defrag_show(struct kobject *kobj,
  301. struct kobj_attribute *attr, char *buf)
  302. {
  303. return double_flag_show(kobj, attr, buf,
  304. TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
  305. TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
  306. }
  307. static ssize_t defrag_store(struct kobject *kobj,
  308. struct kobj_attribute *attr,
  309. const char *buf, size_t count)
  310. {
  311. return double_flag_store(kobj, attr, buf, count,
  312. TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
  313. TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
  314. }
  315. static struct kobj_attribute defrag_attr =
  316. __ATTR(defrag, 0644, defrag_show, defrag_store);
  317. static ssize_t use_zero_page_show(struct kobject *kobj,
  318. struct kobj_attribute *attr, char *buf)
  319. {
  320. return single_flag_show(kobj, attr, buf,
  321. TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
  322. }
  323. static ssize_t use_zero_page_store(struct kobject *kobj,
  324. struct kobj_attribute *attr, const char *buf, size_t count)
  325. {
  326. return single_flag_store(kobj, attr, buf, count,
  327. TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
  328. }
  329. static struct kobj_attribute use_zero_page_attr =
  330. __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
  331. #ifdef CONFIG_DEBUG_VM
  332. static ssize_t debug_cow_show(struct kobject *kobj,
  333. struct kobj_attribute *attr, char *buf)
  334. {
  335. return single_flag_show(kobj, attr, buf,
  336. TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
  337. }
  338. static ssize_t debug_cow_store(struct kobject *kobj,
  339. struct kobj_attribute *attr,
  340. const char *buf, size_t count)
  341. {
  342. return single_flag_store(kobj, attr, buf, count,
  343. TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
  344. }
  345. static struct kobj_attribute debug_cow_attr =
  346. __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
  347. #endif /* CONFIG_DEBUG_VM */
  348. static struct attribute *hugepage_attr[] = {
  349. &enabled_attr.attr,
  350. &defrag_attr.attr,
  351. &use_zero_page_attr.attr,
  352. #ifdef CONFIG_DEBUG_VM
  353. &debug_cow_attr.attr,
  354. #endif
  355. NULL,
  356. };
  357. static struct attribute_group hugepage_attr_group = {
  358. .attrs = hugepage_attr,
  359. };
  360. static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
  361. struct kobj_attribute *attr,
  362. char *buf)
  363. {
  364. return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
  365. }
  366. static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
  367. struct kobj_attribute *attr,
  368. const char *buf, size_t count)
  369. {
  370. unsigned long msecs;
  371. int err;
  372. err = kstrtoul(buf, 10, &msecs);
  373. if (err || msecs > UINT_MAX)
  374. return -EINVAL;
  375. khugepaged_scan_sleep_millisecs = msecs;
  376. wake_up_interruptible(&khugepaged_wait);
  377. return count;
  378. }
  379. static struct kobj_attribute scan_sleep_millisecs_attr =
  380. __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
  381. scan_sleep_millisecs_store);
  382. static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
  383. struct kobj_attribute *attr,
  384. char *buf)
  385. {
  386. return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
  387. }
  388. static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
  389. struct kobj_attribute *attr,
  390. const char *buf, size_t count)
  391. {
  392. unsigned long msecs;
  393. int err;
  394. err = kstrtoul(buf, 10, &msecs);
  395. if (err || msecs > UINT_MAX)
  396. return -EINVAL;
  397. khugepaged_alloc_sleep_millisecs = msecs;
  398. wake_up_interruptible(&khugepaged_wait);
  399. return count;
  400. }
  401. static struct kobj_attribute alloc_sleep_millisecs_attr =
  402. __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
  403. alloc_sleep_millisecs_store);
  404. static ssize_t pages_to_scan_show(struct kobject *kobj,
  405. struct kobj_attribute *attr,
  406. char *buf)
  407. {
  408. return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
  409. }
  410. static ssize_t pages_to_scan_store(struct kobject *kobj,
  411. struct kobj_attribute *attr,
  412. const char *buf, size_t count)
  413. {
  414. int err;
  415. unsigned long pages;
  416. err = kstrtoul(buf, 10, &pages);
  417. if (err || !pages || pages > UINT_MAX)
  418. return -EINVAL;
  419. khugepaged_pages_to_scan = pages;
  420. return count;
  421. }
  422. static struct kobj_attribute pages_to_scan_attr =
  423. __ATTR(pages_to_scan, 0644, pages_to_scan_show,
  424. pages_to_scan_store);
  425. static ssize_t pages_collapsed_show(struct kobject *kobj,
  426. struct kobj_attribute *attr,
  427. char *buf)
  428. {
  429. return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
  430. }
  431. static struct kobj_attribute pages_collapsed_attr =
  432. __ATTR_RO(pages_collapsed);
  433. static ssize_t full_scans_show(struct kobject *kobj,
  434. struct kobj_attribute *attr,
  435. char *buf)
  436. {
  437. return sprintf(buf, "%u\n", khugepaged_full_scans);
  438. }
  439. static struct kobj_attribute full_scans_attr =
  440. __ATTR_RO(full_scans);
  441. static ssize_t khugepaged_defrag_show(struct kobject *kobj,
  442. struct kobj_attribute *attr, char *buf)
  443. {
  444. return single_flag_show(kobj, attr, buf,
  445. TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
  446. }
  447. static ssize_t khugepaged_defrag_store(struct kobject *kobj,
  448. struct kobj_attribute *attr,
  449. const char *buf, size_t count)
  450. {
  451. return single_flag_store(kobj, attr, buf, count,
  452. TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
  453. }
  454. static struct kobj_attribute khugepaged_defrag_attr =
  455. __ATTR(defrag, 0644, khugepaged_defrag_show,
  456. khugepaged_defrag_store);
  457. /*
  458. * max_ptes_none controls if khugepaged should collapse hugepages over
  459. * any unmapped ptes in turn potentially increasing the memory
  460. * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
  461. * reduce the available free memory in the system as it
  462. * runs. Increasing max_ptes_none will instead potentially reduce the
  463. * free memory in the system during the khugepaged scan.
  464. */
  465. static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
  466. struct kobj_attribute *attr,
  467. char *buf)
  468. {
  469. return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
  470. }
  471. static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
  472. struct kobj_attribute *attr,
  473. const char *buf, size_t count)
  474. {
  475. int err;
  476. unsigned long max_ptes_none;
  477. err = kstrtoul(buf, 10, &max_ptes_none);
  478. if (err || max_ptes_none > HPAGE_PMD_NR-1)
  479. return -EINVAL;
  480. khugepaged_max_ptes_none = max_ptes_none;
  481. return count;
  482. }
  483. static struct kobj_attribute khugepaged_max_ptes_none_attr =
  484. __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
  485. khugepaged_max_ptes_none_store);
  486. static struct attribute *khugepaged_attr[] = {
  487. &khugepaged_defrag_attr.attr,
  488. &khugepaged_max_ptes_none_attr.attr,
  489. &pages_to_scan_attr.attr,
  490. &pages_collapsed_attr.attr,
  491. &full_scans_attr.attr,
  492. &scan_sleep_millisecs_attr.attr,
  493. &alloc_sleep_millisecs_attr.attr,
  494. NULL,
  495. };
  496. static struct attribute_group khugepaged_attr_group = {
  497. .attrs = khugepaged_attr,
  498. .name = "khugepaged",
  499. };
  500. static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
  501. {
  502. int err;
  503. *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
  504. if (unlikely(!*hugepage_kobj)) {
  505. pr_err("failed to create transparent hugepage kobject\n");
  506. return -ENOMEM;
  507. }
  508. err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
  509. if (err) {
  510. pr_err("failed to register transparent hugepage group\n");
  511. goto delete_obj;
  512. }
  513. err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
  514. if (err) {
  515. pr_err("failed to register transparent hugepage group\n");
  516. goto remove_hp_group;
  517. }
  518. return 0;
  519. remove_hp_group:
  520. sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
  521. delete_obj:
  522. kobject_put(*hugepage_kobj);
  523. return err;
  524. }
  525. static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
  526. {
  527. sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
  528. sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
  529. kobject_put(hugepage_kobj);
  530. }
  531. #else
  532. static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
  533. {
  534. return 0;
  535. }
  536. static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
  537. {
  538. }
  539. #endif /* CONFIG_SYSFS */
  540. static int __init hugepage_init(void)
  541. {
  542. int err;
  543. struct kobject *hugepage_kobj;
  544. if (!has_transparent_hugepage()) {
  545. transparent_hugepage_flags = 0;
  546. return -EINVAL;
  547. }
  548. err = hugepage_init_sysfs(&hugepage_kobj);
  549. if (err)
  550. goto err_sysfs;
  551. err = khugepaged_slab_init();
  552. if (err)
  553. goto err_slab;
  554. err = register_shrinker(&huge_zero_page_shrinker);
  555. if (err)
  556. goto err_hzp_shrinker;
  557. /*
  558. * By default disable transparent hugepages on smaller systems,
  559. * where the extra memory used could hurt more than TLB overhead
  560. * is likely to save. The admin can still enable it through /sys.
  561. */
  562. if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
  563. transparent_hugepage_flags = 0;
  564. return 0;
  565. }
  566. err = start_stop_khugepaged();
  567. if (err)
  568. goto err_khugepaged;
  569. return 0;
  570. err_khugepaged:
  571. unregister_shrinker(&huge_zero_page_shrinker);
  572. err_hzp_shrinker:
  573. khugepaged_slab_exit();
  574. err_slab:
  575. hugepage_exit_sysfs(hugepage_kobj);
  576. err_sysfs:
  577. return err;
  578. }
  579. subsys_initcall(hugepage_init);
  580. static int __init setup_transparent_hugepage(char *str)
  581. {
  582. int ret = 0;
  583. if (!str)
  584. goto out;
  585. if (!strcmp(str, "always")) {
  586. set_bit(TRANSPARENT_HUGEPAGE_FLAG,
  587. &transparent_hugepage_flags);
  588. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  589. &transparent_hugepage_flags);
  590. ret = 1;
  591. } else if (!strcmp(str, "madvise")) {
  592. clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
  593. &transparent_hugepage_flags);
  594. set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  595. &transparent_hugepage_flags);
  596. ret = 1;
  597. } else if (!strcmp(str, "never")) {
  598. clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
  599. &transparent_hugepage_flags);
  600. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  601. &transparent_hugepage_flags);
  602. ret = 1;
  603. }
  604. out:
  605. if (!ret)
  606. pr_warn("transparent_hugepage= cannot parse, ignored\n");
  607. return ret;
  608. }
  609. __setup("transparent_hugepage=", setup_transparent_hugepage);
  610. pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
  611. {
  612. if (likely(vma->vm_flags & VM_WRITE))
  613. pmd = pmd_mkwrite(pmd);
  614. return pmd;
  615. }
  616. static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
  617. {
  618. pmd_t entry;
  619. entry = mk_pmd(page, prot);
  620. entry = pmd_mkhuge(entry);
  621. return entry;
  622. }
  623. static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
  624. struct vm_area_struct *vma,
  625. unsigned long address, pmd_t *pmd,
  626. struct page *page, gfp_t gfp,
  627. unsigned int flags)
  628. {
  629. struct mem_cgroup *memcg;
  630. pgtable_t pgtable;
  631. spinlock_t *ptl;
  632. unsigned long haddr = address & HPAGE_PMD_MASK;
  633. VM_BUG_ON_PAGE(!PageCompound(page), page);
  634. if (mem_cgroup_try_charge(page, mm, gfp, &memcg)) {
  635. put_page(page);
  636. count_vm_event(THP_FAULT_FALLBACK);
  637. return VM_FAULT_FALLBACK;
  638. }
  639. pgtable = pte_alloc_one(mm, haddr);
  640. if (unlikely(!pgtable)) {
  641. mem_cgroup_cancel_charge(page, memcg);
  642. put_page(page);
  643. return VM_FAULT_OOM;
  644. }
  645. clear_huge_page(page, haddr, HPAGE_PMD_NR);
  646. /*
  647. * The memory barrier inside __SetPageUptodate makes sure that
  648. * clear_huge_page writes become visible before the set_pmd_at()
  649. * write.
  650. */
  651. __SetPageUptodate(page);
  652. ptl = pmd_lock(mm, pmd);
  653. if (unlikely(!pmd_none(*pmd))) {
  654. spin_unlock(ptl);
  655. mem_cgroup_cancel_charge(page, memcg);
  656. put_page(page);
  657. pte_free(mm, pgtable);
  658. } else {
  659. pmd_t entry;
  660. /* Deliver the page fault to userland */
  661. if (userfaultfd_missing(vma)) {
  662. int ret;
  663. spin_unlock(ptl);
  664. mem_cgroup_cancel_charge(page, memcg);
  665. put_page(page);
  666. pte_free(mm, pgtable);
  667. ret = handle_userfault(vma, address, flags,
  668. VM_UFFD_MISSING);
  669. VM_BUG_ON(ret & VM_FAULT_FALLBACK);
  670. return ret;
  671. }
  672. entry = mk_huge_pmd(page, vma->vm_page_prot);
  673. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  674. page_add_new_anon_rmap(page, vma, haddr);
  675. mem_cgroup_commit_charge(page, memcg, false);
  676. lru_cache_add_active_or_unevictable(page, vma);
  677. pgtable_trans_huge_deposit(mm, pmd, pgtable);
  678. set_pmd_at(mm, haddr, pmd, entry);
  679. add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
  680. atomic_long_inc(&mm->nr_ptes);
  681. spin_unlock(ptl);
  682. count_vm_event(THP_FAULT_ALLOC);
  683. }
  684. return 0;
  685. }
  686. static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
  687. {
  688. return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_RECLAIM)) | extra_gfp;
  689. }
  690. /* Caller must hold page table lock. */
  691. static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
  692. struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
  693. struct page *zero_page)
  694. {
  695. pmd_t entry;
  696. if (!pmd_none(*pmd))
  697. return false;
  698. entry = mk_pmd(zero_page, vma->vm_page_prot);
  699. entry = pmd_mkhuge(entry);
  700. pgtable_trans_huge_deposit(mm, pmd, pgtable);
  701. set_pmd_at(mm, haddr, pmd, entry);
  702. atomic_long_inc(&mm->nr_ptes);
  703. return true;
  704. }
  705. int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  706. unsigned long address, pmd_t *pmd,
  707. unsigned int flags)
  708. {
  709. gfp_t gfp;
  710. struct page *page;
  711. unsigned long haddr = address & HPAGE_PMD_MASK;
  712. if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
  713. return VM_FAULT_FALLBACK;
  714. if (unlikely(anon_vma_prepare(vma)))
  715. return VM_FAULT_OOM;
  716. if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
  717. return VM_FAULT_OOM;
  718. if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
  719. transparent_hugepage_use_zero_page()) {
  720. spinlock_t *ptl;
  721. pgtable_t pgtable;
  722. struct page *zero_page;
  723. bool set;
  724. int ret;
  725. pgtable = pte_alloc_one(mm, haddr);
  726. if (unlikely(!pgtable))
  727. return VM_FAULT_OOM;
  728. zero_page = get_huge_zero_page();
  729. if (unlikely(!zero_page)) {
  730. pte_free(mm, pgtable);
  731. count_vm_event(THP_FAULT_FALLBACK);
  732. return VM_FAULT_FALLBACK;
  733. }
  734. ptl = pmd_lock(mm, pmd);
  735. ret = 0;
  736. set = false;
  737. if (pmd_none(*pmd)) {
  738. if (userfaultfd_missing(vma)) {
  739. spin_unlock(ptl);
  740. ret = handle_userfault(vma, address, flags,
  741. VM_UFFD_MISSING);
  742. VM_BUG_ON(ret & VM_FAULT_FALLBACK);
  743. } else {
  744. set_huge_zero_page(pgtable, mm, vma,
  745. haddr, pmd,
  746. zero_page);
  747. spin_unlock(ptl);
  748. set = true;
  749. }
  750. } else
  751. spin_unlock(ptl);
  752. if (!set) {
  753. pte_free(mm, pgtable);
  754. put_huge_zero_page();
  755. }
  756. return ret;
  757. }
  758. gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
  759. page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
  760. if (unlikely(!page)) {
  761. count_vm_event(THP_FAULT_FALLBACK);
  762. return VM_FAULT_FALLBACK;
  763. }
  764. return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
  765. flags);
  766. }
  767. static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
  768. pmd_t *pmd, unsigned long pfn, pgprot_t prot, bool write)
  769. {
  770. struct mm_struct *mm = vma->vm_mm;
  771. pmd_t entry;
  772. spinlock_t *ptl;
  773. ptl = pmd_lock(mm, pmd);
  774. if (pmd_none(*pmd)) {
  775. entry = pmd_mkhuge(pfn_pmd(pfn, prot));
  776. if (write) {
  777. entry = pmd_mkyoung(pmd_mkdirty(entry));
  778. entry = maybe_pmd_mkwrite(entry, vma);
  779. }
  780. set_pmd_at(mm, addr, pmd, entry);
  781. update_mmu_cache_pmd(vma, addr, pmd);
  782. }
  783. spin_unlock(ptl);
  784. }
  785. int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
  786. pmd_t *pmd, unsigned long pfn, bool write)
  787. {
  788. pgprot_t pgprot = vma->vm_page_prot;
  789. /*
  790. * If we had pmd_special, we could avoid all these restrictions,
  791. * but we need to be consistent with PTEs and architectures that
  792. * can't support a 'special' bit.
  793. */
  794. BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
  795. BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
  796. (VM_PFNMAP|VM_MIXEDMAP));
  797. BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
  798. BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
  799. if (addr < vma->vm_start || addr >= vma->vm_end)
  800. return VM_FAULT_SIGBUS;
  801. if (track_pfn_insert(vma, &pgprot, pfn))
  802. return VM_FAULT_SIGBUS;
  803. insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
  804. return VM_FAULT_NOPAGE;
  805. }
  806. int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  807. pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
  808. struct vm_area_struct *vma)
  809. {
  810. spinlock_t *dst_ptl, *src_ptl;
  811. struct page *src_page;
  812. pmd_t pmd;
  813. pgtable_t pgtable;
  814. int ret;
  815. ret = -ENOMEM;
  816. pgtable = pte_alloc_one(dst_mm, addr);
  817. if (unlikely(!pgtable))
  818. goto out;
  819. dst_ptl = pmd_lock(dst_mm, dst_pmd);
  820. src_ptl = pmd_lockptr(src_mm, src_pmd);
  821. spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
  822. ret = -EAGAIN;
  823. pmd = *src_pmd;
  824. if (unlikely(!pmd_trans_huge(pmd))) {
  825. pte_free(dst_mm, pgtable);
  826. goto out_unlock;
  827. }
  828. /*
  829. * When page table lock is held, the huge zero pmd should not be
  830. * under splitting since we don't split the page itself, only pmd to
  831. * a page table.
  832. */
  833. if (is_huge_zero_pmd(pmd)) {
  834. struct page *zero_page;
  835. /*
  836. * get_huge_zero_page() will never allocate a new page here,
  837. * since we already have a zero page to copy. It just takes a
  838. * reference.
  839. */
  840. zero_page = get_huge_zero_page();
  841. set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
  842. zero_page);
  843. ret = 0;
  844. goto out_unlock;
  845. }
  846. if (unlikely(pmd_trans_splitting(pmd))) {
  847. /* split huge page running from under us */
  848. spin_unlock(src_ptl);
  849. spin_unlock(dst_ptl);
  850. pte_free(dst_mm, pgtable);
  851. wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
  852. goto out;
  853. }
  854. src_page = pmd_page(pmd);
  855. VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
  856. get_page(src_page);
  857. page_dup_rmap(src_page);
  858. add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
  859. pmdp_set_wrprotect(src_mm, addr, src_pmd);
  860. pmd = pmd_mkold(pmd_wrprotect(pmd));
  861. pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
  862. set_pmd_at(dst_mm, addr, dst_pmd, pmd);
  863. atomic_long_inc(&dst_mm->nr_ptes);
  864. ret = 0;
  865. out_unlock:
  866. spin_unlock(src_ptl);
  867. spin_unlock(dst_ptl);
  868. out:
  869. return ret;
  870. }
  871. void huge_pmd_set_accessed(struct mm_struct *mm,
  872. struct vm_area_struct *vma,
  873. unsigned long address,
  874. pmd_t *pmd, pmd_t orig_pmd,
  875. int dirty)
  876. {
  877. spinlock_t *ptl;
  878. pmd_t entry;
  879. unsigned long haddr;
  880. ptl = pmd_lock(mm, pmd);
  881. if (unlikely(!pmd_same(*pmd, orig_pmd)))
  882. goto unlock;
  883. entry = pmd_mkyoung(orig_pmd);
  884. haddr = address & HPAGE_PMD_MASK;
  885. if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
  886. update_mmu_cache_pmd(vma, address, pmd);
  887. unlock:
  888. spin_unlock(ptl);
  889. }
  890. /*
  891. * Save CONFIG_DEBUG_PAGEALLOC from faulting falsely on tail pages
  892. * during copy_user_huge_page()'s copy_page_rep(): in the case when
  893. * the source page gets split and a tail freed before copy completes.
  894. * Called under pmd_lock of checked pmd, so safe from splitting itself.
  895. */
  896. static void get_user_huge_page(struct page *page)
  897. {
  898. if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) {
  899. struct page *endpage = page + HPAGE_PMD_NR;
  900. atomic_add(HPAGE_PMD_NR, &page->_count);
  901. while (++page < endpage)
  902. get_huge_page_tail(page);
  903. } else {
  904. get_page(page);
  905. }
  906. }
  907. static void put_user_huge_page(struct page *page)
  908. {
  909. if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) {
  910. struct page *endpage = page + HPAGE_PMD_NR;
  911. while (page < endpage)
  912. put_page(page++);
  913. } else {
  914. put_page(page);
  915. }
  916. }
  917. static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
  918. struct vm_area_struct *vma,
  919. unsigned long address,
  920. pmd_t *pmd, pmd_t orig_pmd,
  921. struct page *page,
  922. unsigned long haddr)
  923. {
  924. struct mem_cgroup *memcg;
  925. spinlock_t *ptl;
  926. pgtable_t pgtable;
  927. pmd_t _pmd;
  928. int ret = 0, i;
  929. struct page **pages;
  930. unsigned long mmun_start; /* For mmu_notifiers */
  931. unsigned long mmun_end; /* For mmu_notifiers */
  932. pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
  933. GFP_KERNEL);
  934. if (unlikely(!pages)) {
  935. ret |= VM_FAULT_OOM;
  936. goto out;
  937. }
  938. for (i = 0; i < HPAGE_PMD_NR; i++) {
  939. pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
  940. __GFP_OTHER_NODE,
  941. vma, address, page_to_nid(page));
  942. if (unlikely(!pages[i] ||
  943. mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
  944. &memcg))) {
  945. if (pages[i])
  946. put_page(pages[i]);
  947. while (--i >= 0) {
  948. memcg = (void *)page_private(pages[i]);
  949. set_page_private(pages[i], 0);
  950. mem_cgroup_cancel_charge(pages[i], memcg);
  951. put_page(pages[i]);
  952. }
  953. kfree(pages);
  954. ret |= VM_FAULT_OOM;
  955. goto out;
  956. }
  957. set_page_private(pages[i], (unsigned long)memcg);
  958. }
  959. for (i = 0; i < HPAGE_PMD_NR; i++) {
  960. copy_user_highpage(pages[i], page + i,
  961. haddr + PAGE_SIZE * i, vma);
  962. __SetPageUptodate(pages[i]);
  963. cond_resched();
  964. }
  965. mmun_start = haddr;
  966. mmun_end = haddr + HPAGE_PMD_SIZE;
  967. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  968. ptl = pmd_lock(mm, pmd);
  969. if (unlikely(!pmd_same(*pmd, orig_pmd)))
  970. goto out_free_pages;
  971. VM_BUG_ON_PAGE(!PageHead(page), page);
  972. pmdp_huge_clear_flush_notify(vma, haddr, pmd);
  973. /* leave pmd empty until pte is filled */
  974. pgtable = pgtable_trans_huge_withdraw(mm, pmd);
  975. pmd_populate(mm, &_pmd, pgtable);
  976. for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
  977. pte_t *pte, entry;
  978. entry = mk_pte(pages[i], vma->vm_page_prot);
  979. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  980. memcg = (void *)page_private(pages[i]);
  981. set_page_private(pages[i], 0);
  982. page_add_new_anon_rmap(pages[i], vma, haddr);
  983. mem_cgroup_commit_charge(pages[i], memcg, false);
  984. lru_cache_add_active_or_unevictable(pages[i], vma);
  985. pte = pte_offset_map(&_pmd, haddr);
  986. VM_BUG_ON(!pte_none(*pte));
  987. set_pte_at(mm, haddr, pte, entry);
  988. pte_unmap(pte);
  989. }
  990. kfree(pages);
  991. smp_wmb(); /* make pte visible before pmd */
  992. pmd_populate(mm, pmd, pgtable);
  993. page_remove_rmap(page);
  994. spin_unlock(ptl);
  995. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  996. ret |= VM_FAULT_WRITE;
  997. put_page(page);
  998. out:
  999. return ret;
  1000. out_free_pages:
  1001. spin_unlock(ptl);
  1002. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1003. for (i = 0; i < HPAGE_PMD_NR; i++) {
  1004. memcg = (void *)page_private(pages[i]);
  1005. set_page_private(pages[i], 0);
  1006. mem_cgroup_cancel_charge(pages[i], memcg);
  1007. put_page(pages[i]);
  1008. }
  1009. kfree(pages);
  1010. goto out;
  1011. }
  1012. int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1013. unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
  1014. {
  1015. spinlock_t *ptl;
  1016. int ret = 0;
  1017. struct page *page = NULL, *new_page;
  1018. struct mem_cgroup *memcg;
  1019. unsigned long haddr;
  1020. unsigned long mmun_start; /* For mmu_notifiers */
  1021. unsigned long mmun_end; /* For mmu_notifiers */
  1022. gfp_t huge_gfp; /* for allocation and charge */
  1023. ptl = pmd_lockptr(mm, pmd);
  1024. VM_BUG_ON_VMA(!vma->anon_vma, vma);
  1025. haddr = address & HPAGE_PMD_MASK;
  1026. if (is_huge_zero_pmd(orig_pmd))
  1027. goto alloc;
  1028. spin_lock(ptl);
  1029. if (unlikely(!pmd_same(*pmd, orig_pmd)))
  1030. goto out_unlock;
  1031. page = pmd_page(orig_pmd);
  1032. VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
  1033. if (page_mapcount(page) == 1) {
  1034. pmd_t entry;
  1035. entry = pmd_mkyoung(orig_pmd);
  1036. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  1037. if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
  1038. update_mmu_cache_pmd(vma, address, pmd);
  1039. ret |= VM_FAULT_WRITE;
  1040. goto out_unlock;
  1041. }
  1042. get_user_huge_page(page);
  1043. spin_unlock(ptl);
  1044. alloc:
  1045. if (transparent_hugepage_enabled(vma) &&
  1046. !transparent_hugepage_debug_cow()) {
  1047. huge_gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
  1048. new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
  1049. } else
  1050. new_page = NULL;
  1051. if (unlikely(!new_page)) {
  1052. if (!page) {
  1053. split_huge_page_pmd(vma, address, pmd);
  1054. ret |= VM_FAULT_FALLBACK;
  1055. } else {
  1056. ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
  1057. pmd, orig_pmd, page, haddr);
  1058. if (ret & VM_FAULT_OOM) {
  1059. split_huge_page(page);
  1060. ret |= VM_FAULT_FALLBACK;
  1061. }
  1062. put_user_huge_page(page);
  1063. }
  1064. count_vm_event(THP_FAULT_FALLBACK);
  1065. goto out;
  1066. }
  1067. if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg))) {
  1068. put_page(new_page);
  1069. if (page) {
  1070. split_huge_page(page);
  1071. put_user_huge_page(page);
  1072. } else
  1073. split_huge_page_pmd(vma, address, pmd);
  1074. ret |= VM_FAULT_FALLBACK;
  1075. count_vm_event(THP_FAULT_FALLBACK);
  1076. goto out;
  1077. }
  1078. count_vm_event(THP_FAULT_ALLOC);
  1079. if (!page)
  1080. clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
  1081. else
  1082. copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
  1083. __SetPageUptodate(new_page);
  1084. mmun_start = haddr;
  1085. mmun_end = haddr + HPAGE_PMD_SIZE;
  1086. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  1087. spin_lock(ptl);
  1088. if (page)
  1089. put_user_huge_page(page);
  1090. if (unlikely(!pmd_same(*pmd, orig_pmd))) {
  1091. spin_unlock(ptl);
  1092. mem_cgroup_cancel_charge(new_page, memcg);
  1093. put_page(new_page);
  1094. goto out_mn;
  1095. } else {
  1096. pmd_t entry;
  1097. entry = mk_huge_pmd(new_page, vma->vm_page_prot);
  1098. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  1099. pmdp_huge_clear_flush_notify(vma, haddr, pmd);
  1100. page_add_new_anon_rmap(new_page, vma, haddr);
  1101. mem_cgroup_commit_charge(new_page, memcg, false);
  1102. lru_cache_add_active_or_unevictable(new_page, vma);
  1103. set_pmd_at(mm, haddr, pmd, entry);
  1104. update_mmu_cache_pmd(vma, address, pmd);
  1105. if (!page) {
  1106. add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
  1107. put_huge_zero_page();
  1108. } else {
  1109. VM_BUG_ON_PAGE(!PageHead(page), page);
  1110. page_remove_rmap(page);
  1111. put_page(page);
  1112. }
  1113. ret |= VM_FAULT_WRITE;
  1114. }
  1115. spin_unlock(ptl);
  1116. out_mn:
  1117. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1118. out:
  1119. return ret;
  1120. out_unlock:
  1121. spin_unlock(ptl);
  1122. return ret;
  1123. }
  1124. /*
  1125. * FOLL_FORCE can write to even unwritable pmd's, but only
  1126. * after we've gone through a COW cycle and they are dirty.
  1127. */
  1128. static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags)
  1129. {
  1130. return pmd_write(pmd) ||
  1131. ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd));
  1132. }
  1133. struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
  1134. unsigned long addr,
  1135. pmd_t *pmd,
  1136. unsigned int flags)
  1137. {
  1138. struct mm_struct *mm = vma->vm_mm;
  1139. struct page *page = NULL;
  1140. assert_spin_locked(pmd_lockptr(mm, pmd));
  1141. if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags))
  1142. goto out;
  1143. /* Avoid dumping huge zero page */
  1144. if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
  1145. return ERR_PTR(-EFAULT);
  1146. /* Full NUMA hinting faults to serialise migration in fault paths */
  1147. if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
  1148. goto out;
  1149. page = pmd_page(*pmd);
  1150. VM_BUG_ON_PAGE(!PageHead(page), page);
  1151. if (flags & FOLL_TOUCH) {
  1152. pmd_t _pmd;
  1153. _pmd = pmd_mkyoung(*pmd);
  1154. if (flags & FOLL_WRITE)
  1155. _pmd = pmd_mkdirty(_pmd);
  1156. if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
  1157. pmd, _pmd, flags & FOLL_WRITE))
  1158. update_mmu_cache_pmd(vma, addr, pmd);
  1159. }
  1160. if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
  1161. if (page->mapping && trylock_page(page)) {
  1162. lru_add_drain();
  1163. if (page->mapping)
  1164. mlock_vma_page(page);
  1165. unlock_page(page);
  1166. }
  1167. }
  1168. page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
  1169. VM_BUG_ON_PAGE(!PageCompound(page), page);
  1170. if (flags & FOLL_GET)
  1171. get_page_foll(page);
  1172. out:
  1173. return page;
  1174. }
  1175. /* NUMA hinting page fault entry point for trans huge pmds */
  1176. int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1177. unsigned long addr, pmd_t pmd, pmd_t *pmdp)
  1178. {
  1179. spinlock_t *ptl;
  1180. struct anon_vma *anon_vma = NULL;
  1181. struct page *page;
  1182. unsigned long haddr = addr & HPAGE_PMD_MASK;
  1183. int page_nid = -1, this_nid = numa_node_id();
  1184. int target_nid, last_cpupid = -1;
  1185. bool page_locked;
  1186. bool migrated = false;
  1187. bool was_writable;
  1188. int flags = 0;
  1189. /* A PROT_NONE fault should not end up here */
  1190. BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
  1191. ptl = pmd_lock(mm, pmdp);
  1192. if (unlikely(!pmd_same(pmd, *pmdp)))
  1193. goto out_unlock;
  1194. /*
  1195. * If there are potential migrations, wait for completion and retry
  1196. * without disrupting NUMA hinting information. Do not relock and
  1197. * check_same as the page may no longer be mapped.
  1198. */
  1199. if (unlikely(pmd_trans_migrating(*pmdp))) {
  1200. page = pmd_page(*pmdp);
  1201. if (!get_page_unless_zero(page))
  1202. goto out_unlock;
  1203. spin_unlock(ptl);
  1204. wait_on_page_locked(page);
  1205. put_page(page);
  1206. goto out;
  1207. }
  1208. page = pmd_page(pmd);
  1209. BUG_ON(is_huge_zero_page(page));
  1210. page_nid = page_to_nid(page);
  1211. last_cpupid = page_cpupid_last(page);
  1212. count_vm_numa_event(NUMA_HINT_FAULTS);
  1213. if (page_nid == this_nid) {
  1214. count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
  1215. flags |= TNF_FAULT_LOCAL;
  1216. }
  1217. /* See similar comment in do_numa_page for explanation */
  1218. if (!(vma->vm_flags & VM_WRITE))
  1219. flags |= TNF_NO_GROUP;
  1220. /*
  1221. * Acquire the page lock to serialise THP migrations but avoid dropping
  1222. * page_table_lock if at all possible
  1223. */
  1224. page_locked = trylock_page(page);
  1225. target_nid = mpol_misplaced(page, vma, haddr);
  1226. if (target_nid == -1) {
  1227. /* If the page was locked, there are no parallel migrations */
  1228. if (page_locked)
  1229. goto clear_pmdnuma;
  1230. }
  1231. /* Migration could have started since the pmd_trans_migrating check */
  1232. if (!page_locked) {
  1233. page_nid = -1;
  1234. if (!get_page_unless_zero(page))
  1235. goto out_unlock;
  1236. spin_unlock(ptl);
  1237. wait_on_page_locked(page);
  1238. put_page(page);
  1239. goto out;
  1240. }
  1241. /*
  1242. * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
  1243. * to serialises splits
  1244. */
  1245. get_page(page);
  1246. spin_unlock(ptl);
  1247. anon_vma = page_lock_anon_vma_read(page);
  1248. /* Confirm the PMD did not change while page_table_lock was released */
  1249. spin_lock(ptl);
  1250. if (unlikely(!pmd_same(pmd, *pmdp))) {
  1251. unlock_page(page);
  1252. put_page(page);
  1253. page_nid = -1;
  1254. goto out_unlock;
  1255. }
  1256. /* Bail if we fail to protect against THP splits for any reason */
  1257. if (unlikely(!anon_vma)) {
  1258. put_page(page);
  1259. page_nid = -1;
  1260. goto clear_pmdnuma;
  1261. }
  1262. /*
  1263. * Migrate the THP to the requested node, returns with page unlocked
  1264. * and access rights restored.
  1265. */
  1266. spin_unlock(ptl);
  1267. migrated = migrate_misplaced_transhuge_page(mm, vma,
  1268. pmdp, pmd, addr, page, target_nid);
  1269. if (migrated) {
  1270. flags |= TNF_MIGRATED;
  1271. page_nid = target_nid;
  1272. } else
  1273. flags |= TNF_MIGRATE_FAIL;
  1274. goto out;
  1275. clear_pmdnuma:
  1276. BUG_ON(!PageLocked(page));
  1277. was_writable = pmd_write(pmd);
  1278. pmd = pmd_modify(pmd, vma->vm_page_prot);
  1279. pmd = pmd_mkyoung(pmd);
  1280. if (was_writable)
  1281. pmd = pmd_mkwrite(pmd);
  1282. set_pmd_at(mm, haddr, pmdp, pmd);
  1283. update_mmu_cache_pmd(vma, addr, pmdp);
  1284. unlock_page(page);
  1285. out_unlock:
  1286. spin_unlock(ptl);
  1287. out:
  1288. if (anon_vma)
  1289. page_unlock_anon_vma_read(anon_vma);
  1290. if (page_nid != -1)
  1291. task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
  1292. return 0;
  1293. }
  1294. int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
  1295. pmd_t *pmd, unsigned long addr)
  1296. {
  1297. pmd_t orig_pmd;
  1298. spinlock_t *ptl;
  1299. if (__pmd_trans_huge_lock(pmd, vma, &ptl) != 1)
  1300. return 0;
  1301. /*
  1302. * For architectures like ppc64 we look at deposited pgtable
  1303. * when calling pmdp_huge_get_and_clear. So do the
  1304. * pgtable_trans_huge_withdraw after finishing pmdp related
  1305. * operations.
  1306. */
  1307. orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
  1308. tlb->fullmm);
  1309. tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
  1310. if (vma_is_dax(vma)) {
  1311. spin_unlock(ptl);
  1312. if (is_huge_zero_pmd(orig_pmd))
  1313. put_huge_zero_page();
  1314. } else if (is_huge_zero_pmd(orig_pmd)) {
  1315. pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
  1316. atomic_long_dec(&tlb->mm->nr_ptes);
  1317. spin_unlock(ptl);
  1318. put_huge_zero_page();
  1319. } else {
  1320. struct page *page = pmd_page(orig_pmd);
  1321. page_remove_rmap(page);
  1322. VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
  1323. add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
  1324. VM_BUG_ON_PAGE(!PageHead(page), page);
  1325. pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
  1326. atomic_long_dec(&tlb->mm->nr_ptes);
  1327. spin_unlock(ptl);
  1328. tlb_remove_page(tlb, page);
  1329. }
  1330. return 1;
  1331. }
  1332. int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
  1333. unsigned long old_addr,
  1334. unsigned long new_addr, unsigned long old_end,
  1335. pmd_t *old_pmd, pmd_t *new_pmd)
  1336. {
  1337. spinlock_t *old_ptl, *new_ptl;
  1338. int ret = 0;
  1339. pmd_t pmd;
  1340. bool force_flush = false;
  1341. struct mm_struct *mm = vma->vm_mm;
  1342. if ((old_addr & ~HPAGE_PMD_MASK) ||
  1343. (new_addr & ~HPAGE_PMD_MASK) ||
  1344. old_end - old_addr < HPAGE_PMD_SIZE ||
  1345. (new_vma->vm_flags & VM_NOHUGEPAGE))
  1346. goto out;
  1347. /*
  1348. * The destination pmd shouldn't be established, free_pgtables()
  1349. * should have release it.
  1350. */
  1351. if (WARN_ON(!pmd_none(*new_pmd))) {
  1352. VM_BUG_ON(pmd_trans_huge(*new_pmd));
  1353. goto out;
  1354. }
  1355. /*
  1356. * We don't have to worry about the ordering of src and dst
  1357. * ptlocks because exclusive mmap_sem prevents deadlock.
  1358. */
  1359. ret = __pmd_trans_huge_lock(old_pmd, vma, &old_ptl);
  1360. if (ret == 1) {
  1361. new_ptl = pmd_lockptr(mm, new_pmd);
  1362. if (new_ptl != old_ptl)
  1363. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  1364. pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
  1365. if (pmd_present(pmd))
  1366. force_flush = true;
  1367. VM_BUG_ON(!pmd_none(*new_pmd));
  1368. if (pmd_move_must_withdraw(new_ptl, old_ptl)) {
  1369. pgtable_t pgtable;
  1370. pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
  1371. pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
  1372. }
  1373. set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
  1374. if (force_flush)
  1375. flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
  1376. if (new_ptl != old_ptl)
  1377. spin_unlock(new_ptl);
  1378. spin_unlock(old_ptl);
  1379. }
  1380. out:
  1381. return ret;
  1382. }
  1383. /*
  1384. * Returns
  1385. * - 0 if PMD could not be locked
  1386. * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
  1387. * - HPAGE_PMD_NR is protections changed and TLB flush necessary
  1388. */
  1389. int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
  1390. unsigned long addr, pgprot_t newprot, int prot_numa)
  1391. {
  1392. struct mm_struct *mm = vma->vm_mm;
  1393. spinlock_t *ptl;
  1394. pmd_t entry;
  1395. bool preserve_write;
  1396. int ret = 0;
  1397. if (__pmd_trans_huge_lock(pmd, vma, &ptl) != 1)
  1398. return 0;
  1399. preserve_write = prot_numa && pmd_write(*pmd);
  1400. ret = 1;
  1401. /*
  1402. * Avoid trapping faults against the zero page. The read-only
  1403. * data is likely to be read-cached on the local CPU and
  1404. * local/remote hits to the zero page are not interesting.
  1405. */
  1406. if (prot_numa && is_huge_zero_pmd(*pmd))
  1407. goto unlock;
  1408. if (prot_numa && pmd_protnone(*pmd))
  1409. goto unlock;
  1410. /*
  1411. * In case prot_numa, we are under down_read(mmap_sem). It's critical
  1412. * to not clear pmd intermittently to avoid race with MADV_DONTNEED
  1413. * which is also under down_read(mmap_sem):
  1414. *
  1415. * CPU0: CPU1:
  1416. * change_huge_pmd(prot_numa=1)
  1417. * pmdp_huge_get_and_clear_notify()
  1418. * madvise_dontneed()
  1419. * zap_pmd_range()
  1420. * pmd_trans_huge(*pmd) == 0 (without ptl)
  1421. * // skip the pmd
  1422. * set_pmd_at();
  1423. * // pmd is re-established
  1424. *
  1425. * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
  1426. * which may break userspace.
  1427. *
  1428. * pmdp_invalidate() is required to make sure we don't miss
  1429. * dirty/young flags set by hardware.
  1430. */
  1431. entry = *pmd;
  1432. pmdp_invalidate(vma, addr, pmd);
  1433. /*
  1434. * Recover dirty/young flags. It relies on pmdp_invalidate to not
  1435. * corrupt them.
  1436. */
  1437. if (pmd_dirty(*pmd))
  1438. entry = pmd_mkdirty(entry);
  1439. if (pmd_young(*pmd))
  1440. entry = pmd_mkyoung(entry);
  1441. entry = pmd_modify(entry, newprot);
  1442. if (preserve_write)
  1443. entry = pmd_mkwrite(entry);
  1444. ret = HPAGE_PMD_NR;
  1445. set_pmd_at(mm, addr, pmd, entry);
  1446. BUG_ON(!preserve_write && pmd_write(entry));
  1447. unlock:
  1448. spin_unlock(ptl);
  1449. return ret;
  1450. }
  1451. /*
  1452. * Returns 1 if a given pmd maps a stable (not under splitting) thp.
  1453. * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
  1454. *
  1455. * Note that if it returns 1, this routine returns without unlocking page
  1456. * table locks. So callers must unlock them.
  1457. */
  1458. int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma,
  1459. spinlock_t **ptl)
  1460. {
  1461. *ptl = pmd_lock(vma->vm_mm, pmd);
  1462. if (likely(pmd_trans_huge(*pmd))) {
  1463. if (unlikely(pmd_trans_splitting(*pmd))) {
  1464. spin_unlock(*ptl);
  1465. wait_split_huge_page(vma->anon_vma, pmd);
  1466. return -1;
  1467. } else {
  1468. /* Thp mapped by 'pmd' is stable, so we can
  1469. * handle it as it is. */
  1470. return 1;
  1471. }
  1472. }
  1473. spin_unlock(*ptl);
  1474. return 0;
  1475. }
  1476. /*
  1477. * This function returns whether a given @page is mapped onto the @address
  1478. * in the virtual space of @mm.
  1479. *
  1480. * When it's true, this function returns *pmd with holding the page table lock
  1481. * and passing it back to the caller via @ptl.
  1482. * If it's false, returns NULL without holding the page table lock.
  1483. */
  1484. pmd_t *page_check_address_pmd(struct page *page,
  1485. struct mm_struct *mm,
  1486. unsigned long address,
  1487. enum page_check_address_pmd_flag flag,
  1488. spinlock_t **ptl)
  1489. {
  1490. pgd_t *pgd;
  1491. pud_t *pud;
  1492. pmd_t *pmd;
  1493. if (address & ~HPAGE_PMD_MASK)
  1494. return NULL;
  1495. pgd = pgd_offset(mm, address);
  1496. if (!pgd_present(*pgd))
  1497. return NULL;
  1498. pud = pud_offset(pgd, address);
  1499. if (!pud_present(*pud))
  1500. return NULL;
  1501. pmd = pmd_offset(pud, address);
  1502. *ptl = pmd_lock(mm, pmd);
  1503. if (!pmd_present(*pmd))
  1504. goto unlock;
  1505. if (pmd_page(*pmd) != page)
  1506. goto unlock;
  1507. /*
  1508. * split_vma() may create temporary aliased mappings. There is
  1509. * no risk as long as all huge pmd are found and have their
  1510. * splitting bit set before __split_huge_page_refcount
  1511. * runs. Finding the same huge pmd more than once during the
  1512. * same rmap walk is not a problem.
  1513. */
  1514. if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
  1515. pmd_trans_splitting(*pmd))
  1516. goto unlock;
  1517. if (pmd_trans_huge(*pmd)) {
  1518. VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
  1519. !pmd_trans_splitting(*pmd));
  1520. return pmd;
  1521. }
  1522. unlock:
  1523. spin_unlock(*ptl);
  1524. return NULL;
  1525. }
  1526. static int __split_huge_page_splitting(struct page *page,
  1527. struct vm_area_struct *vma,
  1528. unsigned long address)
  1529. {
  1530. struct mm_struct *mm = vma->vm_mm;
  1531. spinlock_t *ptl;
  1532. pmd_t *pmd;
  1533. int ret = 0;
  1534. /* For mmu_notifiers */
  1535. const unsigned long mmun_start = address;
  1536. const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
  1537. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  1538. pmd = page_check_address_pmd(page, mm, address,
  1539. PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG, &ptl);
  1540. if (pmd) {
  1541. /*
  1542. * We can't temporarily set the pmd to null in order
  1543. * to split it, the pmd must remain marked huge at all
  1544. * times or the VM won't take the pmd_trans_huge paths
  1545. * and it won't wait on the anon_vma->root->rwsem to
  1546. * serialize against split_huge_page*.
  1547. */
  1548. pmdp_splitting_flush(vma, address, pmd);
  1549. ret = 1;
  1550. spin_unlock(ptl);
  1551. }
  1552. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1553. return ret;
  1554. }
  1555. static void __split_huge_page_refcount(struct page *page,
  1556. struct list_head *list)
  1557. {
  1558. int i;
  1559. struct zone *zone = page_zone(page);
  1560. struct lruvec *lruvec;
  1561. int tail_count = 0;
  1562. /* prevent PageLRU to go away from under us, and freeze lru stats */
  1563. spin_lock_irq(&zone->lru_lock);
  1564. lruvec = mem_cgroup_page_lruvec(page, zone);
  1565. compound_lock(page);
  1566. /* complete memcg works before add pages to LRU */
  1567. mem_cgroup_split_huge_fixup(page);
  1568. for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
  1569. struct page *page_tail = page + i;
  1570. /* tail_page->_mapcount cannot change */
  1571. BUG_ON(page_mapcount(page_tail) < 0);
  1572. tail_count += page_mapcount(page_tail);
  1573. /* check for overflow */
  1574. BUG_ON(tail_count < 0);
  1575. BUG_ON(atomic_read(&page_tail->_count) != 0);
  1576. /*
  1577. * tail_page->_count is zero and not changing from
  1578. * under us. But get_page_unless_zero() may be running
  1579. * from under us on the tail_page. If we used
  1580. * atomic_set() below instead of atomic_add(), we
  1581. * would then run atomic_set() concurrently with
  1582. * get_page_unless_zero(), and atomic_set() is
  1583. * implemented in C not using locked ops. spin_unlock
  1584. * on x86 sometime uses locked ops because of PPro
  1585. * errata 66, 92, so unless somebody can guarantee
  1586. * atomic_set() here would be safe on all archs (and
  1587. * not only on x86), it's safer to use atomic_add().
  1588. */
  1589. atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
  1590. &page_tail->_count);
  1591. /* after clearing PageTail the gup refcount can be released */
  1592. smp_mb__after_atomic();
  1593. page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
  1594. page_tail->flags |= (page->flags &
  1595. ((1L << PG_referenced) |
  1596. (1L << PG_swapbacked) |
  1597. (1L << PG_mlocked) |
  1598. (1L << PG_uptodate) |
  1599. (1L << PG_active) |
  1600. (1L << PG_unevictable)));
  1601. page_tail->flags |= (1L << PG_dirty);
  1602. clear_compound_head(page_tail);
  1603. if (page_is_young(page))
  1604. set_page_young(page_tail);
  1605. if (page_is_idle(page))
  1606. set_page_idle(page_tail);
  1607. /*
  1608. * __split_huge_page_splitting() already set the
  1609. * splitting bit in all pmd that could map this
  1610. * hugepage, that will ensure no CPU can alter the
  1611. * mapcount on the head page. The mapcount is only
  1612. * accounted in the head page and it has to be
  1613. * transferred to all tail pages in the below code. So
  1614. * for this code to be safe, the split the mapcount
  1615. * can't change. But that doesn't mean userland can't
  1616. * keep changing and reading the page contents while
  1617. * we transfer the mapcount, so the pmd splitting
  1618. * status is achieved setting a reserved bit in the
  1619. * pmd, not by clearing the present bit.
  1620. */
  1621. page_tail->_mapcount = page->_mapcount;
  1622. BUG_ON(page_tail->mapping);
  1623. page_tail->mapping = page->mapping;
  1624. page_tail->index = page->index + i;
  1625. page_cpupid_xchg_last(page_tail, page_cpupid_last(page));
  1626. BUG_ON(!PageAnon(page_tail));
  1627. BUG_ON(!PageUptodate(page_tail));
  1628. BUG_ON(!PageDirty(page_tail));
  1629. BUG_ON(!PageSwapBacked(page_tail));
  1630. lru_add_page_tail(page, page_tail, lruvec, list);
  1631. }
  1632. atomic_sub(tail_count, &page->_count);
  1633. BUG_ON(atomic_read(&page->_count) <= 0);
  1634. __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
  1635. ClearPageCompound(page);
  1636. compound_unlock(page);
  1637. spin_unlock_irq(&zone->lru_lock);
  1638. for (i = 1; i < HPAGE_PMD_NR; i++) {
  1639. struct page *page_tail = page + i;
  1640. BUG_ON(page_count(page_tail) <= 0);
  1641. /*
  1642. * Tail pages may be freed if there wasn't any mapping
  1643. * like if add_to_swap() is running on a lru page that
  1644. * had its mapping zapped. And freeing these pages
  1645. * requires taking the lru_lock so we do the put_page
  1646. * of the tail pages after the split is complete.
  1647. */
  1648. put_page(page_tail);
  1649. }
  1650. /*
  1651. * Only the head page (now become a regular page) is required
  1652. * to be pinned by the caller.
  1653. */
  1654. BUG_ON(page_count(page) <= 0);
  1655. }
  1656. static int __split_huge_page_map(struct page *page,
  1657. struct vm_area_struct *vma,
  1658. unsigned long address)
  1659. {
  1660. struct mm_struct *mm = vma->vm_mm;
  1661. spinlock_t *ptl;
  1662. pmd_t *pmd, _pmd;
  1663. int ret = 0, i;
  1664. pgtable_t pgtable;
  1665. unsigned long haddr;
  1666. pmd = page_check_address_pmd(page, mm, address,
  1667. PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG, &ptl);
  1668. if (pmd) {
  1669. pgtable = pgtable_trans_huge_withdraw(mm, pmd);
  1670. pmd_populate(mm, &_pmd, pgtable);
  1671. if (pmd_write(*pmd))
  1672. BUG_ON(page_mapcount(page) != 1);
  1673. haddr = address;
  1674. for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
  1675. pte_t *pte, entry;
  1676. BUG_ON(PageCompound(page+i));
  1677. /*
  1678. * Note that NUMA hinting access restrictions are not
  1679. * transferred to avoid any possibility of altering
  1680. * permissions across VMAs.
  1681. */
  1682. entry = mk_pte(page + i, vma->vm_page_prot);
  1683. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1684. if (!pmd_write(*pmd))
  1685. entry = pte_wrprotect(entry);
  1686. if (!pmd_young(*pmd))
  1687. entry = pte_mkold(entry);
  1688. pte = pte_offset_map(&_pmd, haddr);
  1689. BUG_ON(!pte_none(*pte));
  1690. set_pte_at(mm, haddr, pte, entry);
  1691. pte_unmap(pte);
  1692. }
  1693. smp_wmb(); /* make pte visible before pmd */
  1694. /*
  1695. * Up to this point the pmd is present and huge and
  1696. * userland has the whole access to the hugepage
  1697. * during the split (which happens in place). If we
  1698. * overwrite the pmd with the not-huge version
  1699. * pointing to the pte here (which of course we could
  1700. * if all CPUs were bug free), userland could trigger
  1701. * a small page size TLB miss on the small sized TLB
  1702. * while the hugepage TLB entry is still established
  1703. * in the huge TLB. Some CPU doesn't like that. See
  1704. * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
  1705. * Erratum 383 on page 93. Intel should be safe but is
  1706. * also warns that it's only safe if the permission
  1707. * and cache attributes of the two entries loaded in
  1708. * the two TLB is identical (which should be the case
  1709. * here). But it is generally safer to never allow
  1710. * small and huge TLB entries for the same virtual
  1711. * address to be loaded simultaneously. So instead of
  1712. * doing "pmd_populate(); flush_pmd_tlb_range();" we first
  1713. * mark the current pmd notpresent (atomically because
  1714. * here the pmd_trans_huge and pmd_trans_splitting
  1715. * must remain set at all times on the pmd until the
  1716. * split is complete for this pmd), then we flush the
  1717. * SMP TLB and finally we write the non-huge version
  1718. * of the pmd entry with pmd_populate.
  1719. */
  1720. pmdp_invalidate(vma, address, pmd);
  1721. pmd_populate(mm, pmd, pgtable);
  1722. ret = 1;
  1723. spin_unlock(ptl);
  1724. }
  1725. return ret;
  1726. }
  1727. /* must be called with anon_vma->root->rwsem held */
  1728. static void __split_huge_page(struct page *page,
  1729. struct anon_vma *anon_vma,
  1730. struct list_head *list)
  1731. {
  1732. int mapcount, mapcount2;
  1733. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  1734. struct anon_vma_chain *avc;
  1735. BUG_ON(!PageHead(page));
  1736. BUG_ON(PageTail(page));
  1737. mapcount = 0;
  1738. anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
  1739. struct vm_area_struct *vma = avc->vma;
  1740. unsigned long addr = vma_address(page, vma);
  1741. BUG_ON(is_vma_temporary_stack(vma));
  1742. mapcount += __split_huge_page_splitting(page, vma, addr);
  1743. }
  1744. /*
  1745. * It is critical that new vmas are added to the tail of the
  1746. * anon_vma list. This guarantes that if copy_huge_pmd() runs
  1747. * and establishes a child pmd before
  1748. * __split_huge_page_splitting() freezes the parent pmd (so if
  1749. * we fail to prevent copy_huge_pmd() from running until the
  1750. * whole __split_huge_page() is complete), we will still see
  1751. * the newly established pmd of the child later during the
  1752. * walk, to be able to set it as pmd_trans_splitting too.
  1753. */
  1754. if (mapcount != page_mapcount(page)) {
  1755. pr_err("mapcount %d page_mapcount %d\n",
  1756. mapcount, page_mapcount(page));
  1757. BUG();
  1758. }
  1759. __split_huge_page_refcount(page, list);
  1760. mapcount2 = 0;
  1761. anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
  1762. struct vm_area_struct *vma = avc->vma;
  1763. unsigned long addr = vma_address(page, vma);
  1764. BUG_ON(is_vma_temporary_stack(vma));
  1765. mapcount2 += __split_huge_page_map(page, vma, addr);
  1766. }
  1767. if (mapcount != mapcount2) {
  1768. pr_err("mapcount %d mapcount2 %d page_mapcount %d\n",
  1769. mapcount, mapcount2, page_mapcount(page));
  1770. BUG();
  1771. }
  1772. }
  1773. /*
  1774. * Split a hugepage into normal pages. This doesn't change the position of head
  1775. * page. If @list is null, tail pages will be added to LRU list, otherwise, to
  1776. * @list. Both head page and tail pages will inherit mapping, flags, and so on
  1777. * from the hugepage.
  1778. * Return 0 if the hugepage is split successfully otherwise return 1.
  1779. */
  1780. int split_huge_page_to_list(struct page *page, struct list_head *list)
  1781. {
  1782. struct anon_vma *anon_vma;
  1783. int ret = 1;
  1784. BUG_ON(is_huge_zero_page(page));
  1785. BUG_ON(!PageAnon(page));
  1786. /*
  1787. * The caller does not necessarily hold an mmap_sem that would prevent
  1788. * the anon_vma disappearing so we first we take a reference to it
  1789. * and then lock the anon_vma for write. This is similar to
  1790. * page_lock_anon_vma_read except the write lock is taken to serialise
  1791. * against parallel split or collapse operations.
  1792. */
  1793. anon_vma = page_get_anon_vma(page);
  1794. if (!anon_vma)
  1795. goto out;
  1796. anon_vma_lock_write(anon_vma);
  1797. ret = 0;
  1798. if (!PageCompound(page))
  1799. goto out_unlock;
  1800. BUG_ON(!PageSwapBacked(page));
  1801. __split_huge_page(page, anon_vma, list);
  1802. count_vm_event(THP_SPLIT);
  1803. BUG_ON(PageCompound(page));
  1804. out_unlock:
  1805. anon_vma_unlock_write(anon_vma);
  1806. put_anon_vma(anon_vma);
  1807. out:
  1808. return ret;
  1809. }
  1810. #define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
  1811. int hugepage_madvise(struct vm_area_struct *vma,
  1812. unsigned long *vm_flags, int advice)
  1813. {
  1814. switch (advice) {
  1815. case MADV_HUGEPAGE:
  1816. #ifdef CONFIG_S390
  1817. /*
  1818. * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
  1819. * can't handle this properly after s390_enable_sie, so we simply
  1820. * ignore the madvise to prevent qemu from causing a SIGSEGV.
  1821. */
  1822. if (mm_has_pgste(vma->vm_mm))
  1823. return 0;
  1824. #endif
  1825. /*
  1826. * Be somewhat over-protective like KSM for now!
  1827. */
  1828. if (*vm_flags & VM_NO_THP)
  1829. return -EINVAL;
  1830. *vm_flags &= ~VM_NOHUGEPAGE;
  1831. *vm_flags |= VM_HUGEPAGE;
  1832. /*
  1833. * If the vma become good for khugepaged to scan,
  1834. * register it here without waiting a page fault that
  1835. * may not happen any time soon.
  1836. */
  1837. if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
  1838. return -ENOMEM;
  1839. break;
  1840. case MADV_NOHUGEPAGE:
  1841. /*
  1842. * Be somewhat over-protective like KSM for now!
  1843. */
  1844. if (*vm_flags & VM_NO_THP)
  1845. return -EINVAL;
  1846. *vm_flags &= ~VM_HUGEPAGE;
  1847. *vm_flags |= VM_NOHUGEPAGE;
  1848. /*
  1849. * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
  1850. * this vma even if we leave the mm registered in khugepaged if
  1851. * it got registered before VM_NOHUGEPAGE was set.
  1852. */
  1853. break;
  1854. }
  1855. return 0;
  1856. }
  1857. static int __init khugepaged_slab_init(void)
  1858. {
  1859. mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
  1860. sizeof(struct mm_slot),
  1861. __alignof__(struct mm_slot), 0, NULL);
  1862. if (!mm_slot_cache)
  1863. return -ENOMEM;
  1864. return 0;
  1865. }
  1866. static void __init khugepaged_slab_exit(void)
  1867. {
  1868. kmem_cache_destroy(mm_slot_cache);
  1869. }
  1870. static inline struct mm_slot *alloc_mm_slot(void)
  1871. {
  1872. if (!mm_slot_cache) /* initialization failed */
  1873. return NULL;
  1874. return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
  1875. }
  1876. static inline void free_mm_slot(struct mm_slot *mm_slot)
  1877. {
  1878. kmem_cache_free(mm_slot_cache, mm_slot);
  1879. }
  1880. static struct mm_slot *get_mm_slot(struct mm_struct *mm)
  1881. {
  1882. struct mm_slot *mm_slot;
  1883. hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
  1884. if (mm == mm_slot->mm)
  1885. return mm_slot;
  1886. return NULL;
  1887. }
  1888. static void insert_to_mm_slots_hash(struct mm_struct *mm,
  1889. struct mm_slot *mm_slot)
  1890. {
  1891. mm_slot->mm = mm;
  1892. hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
  1893. }
  1894. static inline int khugepaged_test_exit(struct mm_struct *mm)
  1895. {
  1896. return atomic_read(&mm->mm_users) == 0;
  1897. }
  1898. int __khugepaged_enter(struct mm_struct *mm)
  1899. {
  1900. struct mm_slot *mm_slot;
  1901. int wakeup;
  1902. mm_slot = alloc_mm_slot();
  1903. if (!mm_slot)
  1904. return -ENOMEM;
  1905. /* __khugepaged_exit() must not run from under us */
  1906. VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
  1907. if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
  1908. free_mm_slot(mm_slot);
  1909. return 0;
  1910. }
  1911. spin_lock(&khugepaged_mm_lock);
  1912. insert_to_mm_slots_hash(mm, mm_slot);
  1913. /*
  1914. * Insert just behind the scanning cursor, to let the area settle
  1915. * down a little.
  1916. */
  1917. wakeup = list_empty(&khugepaged_scan.mm_head);
  1918. list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
  1919. spin_unlock(&khugepaged_mm_lock);
  1920. atomic_inc(&mm->mm_count);
  1921. if (wakeup)
  1922. wake_up_interruptible(&khugepaged_wait);
  1923. return 0;
  1924. }
  1925. int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
  1926. unsigned long vm_flags)
  1927. {
  1928. unsigned long hstart, hend;
  1929. if (!vma->anon_vma)
  1930. /*
  1931. * Not yet faulted in so we will register later in the
  1932. * page fault if needed.
  1933. */
  1934. return 0;
  1935. if (vma->vm_ops || (vm_flags & VM_NO_THP))
  1936. /* khugepaged not yet working on file or special mappings */
  1937. return 0;
  1938. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  1939. hend = vma->vm_end & HPAGE_PMD_MASK;
  1940. if (hstart < hend)
  1941. return khugepaged_enter(vma, vm_flags);
  1942. return 0;
  1943. }
  1944. void __khugepaged_exit(struct mm_struct *mm)
  1945. {
  1946. struct mm_slot *mm_slot;
  1947. int free = 0;
  1948. spin_lock(&khugepaged_mm_lock);
  1949. mm_slot = get_mm_slot(mm);
  1950. if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
  1951. hash_del(&mm_slot->hash);
  1952. list_del(&mm_slot->mm_node);
  1953. free = 1;
  1954. }
  1955. spin_unlock(&khugepaged_mm_lock);
  1956. if (free) {
  1957. clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
  1958. free_mm_slot(mm_slot);
  1959. mmdrop(mm);
  1960. } else if (mm_slot) {
  1961. /*
  1962. * This is required to serialize against
  1963. * khugepaged_test_exit() (which is guaranteed to run
  1964. * under mmap sem read mode). Stop here (after we
  1965. * return all pagetables will be destroyed) until
  1966. * khugepaged has finished working on the pagetables
  1967. * under the mmap_sem.
  1968. */
  1969. down_write(&mm->mmap_sem);
  1970. up_write(&mm->mmap_sem);
  1971. }
  1972. }
  1973. static void release_pte_page(struct page *page)
  1974. {
  1975. /* 0 stands for page_is_file_cache(page) == false */
  1976. dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
  1977. unlock_page(page);
  1978. putback_lru_page(page);
  1979. }
  1980. static void release_pte_pages(pte_t *pte, pte_t *_pte)
  1981. {
  1982. while (--_pte >= pte) {
  1983. pte_t pteval = *_pte;
  1984. if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
  1985. release_pte_page(pte_page(pteval));
  1986. }
  1987. }
  1988. static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
  1989. unsigned long address,
  1990. pte_t *pte)
  1991. {
  1992. struct page *page;
  1993. pte_t *_pte;
  1994. int none_or_zero = 0;
  1995. bool referenced = false, writable = false;
  1996. for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
  1997. _pte++, address += PAGE_SIZE) {
  1998. pte_t pteval = *_pte;
  1999. if (pte_none(pteval) || (pte_present(pteval) &&
  2000. is_zero_pfn(pte_pfn(pteval)))) {
  2001. if (!userfaultfd_armed(vma) &&
  2002. ++none_or_zero <= khugepaged_max_ptes_none)
  2003. continue;
  2004. else
  2005. goto out;
  2006. }
  2007. if (!pte_present(pteval))
  2008. goto out;
  2009. page = vm_normal_page(vma, address, pteval);
  2010. if (unlikely(!page))
  2011. goto out;
  2012. VM_BUG_ON_PAGE(PageCompound(page), page);
  2013. VM_BUG_ON_PAGE(!PageAnon(page), page);
  2014. VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
  2015. /*
  2016. * We can do it before isolate_lru_page because the
  2017. * page can't be freed from under us. NOTE: PG_lock
  2018. * is needed to serialize against split_huge_page
  2019. * when invoked from the VM.
  2020. */
  2021. if (!trylock_page(page))
  2022. goto out;
  2023. /*
  2024. * cannot use mapcount: can't collapse if there's a gup pin.
  2025. * The page must only be referenced by the scanned process
  2026. * and page swap cache.
  2027. */
  2028. if (page_count(page) != 1 + !!PageSwapCache(page)) {
  2029. unlock_page(page);
  2030. goto out;
  2031. }
  2032. if (pte_write(pteval)) {
  2033. writable = true;
  2034. } else {
  2035. if (PageSwapCache(page) && !reuse_swap_page(page)) {
  2036. unlock_page(page);
  2037. goto out;
  2038. }
  2039. /*
  2040. * Page is not in the swap cache. It can be collapsed
  2041. * into a THP.
  2042. */
  2043. }
  2044. /*
  2045. * Isolate the page to avoid collapsing an hugepage
  2046. * currently in use by the VM.
  2047. */
  2048. if (isolate_lru_page(page)) {
  2049. unlock_page(page);
  2050. goto out;
  2051. }
  2052. /* 0 stands for page_is_file_cache(page) == false */
  2053. inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
  2054. VM_BUG_ON_PAGE(!PageLocked(page), page);
  2055. VM_BUG_ON_PAGE(PageLRU(page), page);
  2056. /* If there is no mapped pte young don't collapse the page */
  2057. if (pte_young(pteval) ||
  2058. page_is_young(page) || PageReferenced(page) ||
  2059. mmu_notifier_test_young(vma->vm_mm, address))
  2060. referenced = true;
  2061. }
  2062. if (likely(referenced && writable))
  2063. return 1;
  2064. out:
  2065. release_pte_pages(pte, _pte);
  2066. return 0;
  2067. }
  2068. static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
  2069. struct vm_area_struct *vma,
  2070. unsigned long address,
  2071. spinlock_t *ptl)
  2072. {
  2073. pte_t *_pte;
  2074. for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
  2075. pte_t pteval = *_pte;
  2076. struct page *src_page;
  2077. if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
  2078. clear_user_highpage(page, address);
  2079. add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
  2080. if (is_zero_pfn(pte_pfn(pteval))) {
  2081. /*
  2082. * ptl mostly unnecessary.
  2083. */
  2084. spin_lock(ptl);
  2085. /*
  2086. * paravirt calls inside pte_clear here are
  2087. * superfluous.
  2088. */
  2089. pte_clear(vma->vm_mm, address, _pte);
  2090. spin_unlock(ptl);
  2091. }
  2092. } else {
  2093. src_page = pte_page(pteval);
  2094. copy_user_highpage(page, src_page, address, vma);
  2095. VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
  2096. release_pte_page(src_page);
  2097. /*
  2098. * ptl mostly unnecessary, but preempt has to
  2099. * be disabled to update the per-cpu stats
  2100. * inside page_remove_rmap().
  2101. */
  2102. spin_lock(ptl);
  2103. /*
  2104. * paravirt calls inside pte_clear here are
  2105. * superfluous.
  2106. */
  2107. pte_clear(vma->vm_mm, address, _pte);
  2108. page_remove_rmap(src_page);
  2109. spin_unlock(ptl);
  2110. free_page_and_swap_cache(src_page);
  2111. }
  2112. address += PAGE_SIZE;
  2113. page++;
  2114. }
  2115. }
  2116. static void khugepaged_alloc_sleep(void)
  2117. {
  2118. DEFINE_WAIT(wait);
  2119. add_wait_queue(&khugepaged_wait, &wait);
  2120. freezable_schedule_timeout_interruptible(
  2121. msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
  2122. remove_wait_queue(&khugepaged_wait, &wait);
  2123. }
  2124. static int khugepaged_node_load[MAX_NUMNODES];
  2125. static bool khugepaged_scan_abort(int nid)
  2126. {
  2127. int i;
  2128. /*
  2129. * If zone_reclaim_mode is disabled, then no extra effort is made to
  2130. * allocate memory locally.
  2131. */
  2132. if (!zone_reclaim_mode)
  2133. return false;
  2134. /* If there is a count for this node already, it must be acceptable */
  2135. if (khugepaged_node_load[nid])
  2136. return false;
  2137. for (i = 0; i < MAX_NUMNODES; i++) {
  2138. if (!khugepaged_node_load[i])
  2139. continue;
  2140. if (node_distance(nid, i) > RECLAIM_DISTANCE)
  2141. return true;
  2142. }
  2143. return false;
  2144. }
  2145. #ifdef CONFIG_NUMA
  2146. static int khugepaged_find_target_node(void)
  2147. {
  2148. static int last_khugepaged_target_node = NUMA_NO_NODE;
  2149. int nid, target_node = 0, max_value = 0;
  2150. /* find first node with max normal pages hit */
  2151. for (nid = 0; nid < MAX_NUMNODES; nid++)
  2152. if (khugepaged_node_load[nid] > max_value) {
  2153. max_value = khugepaged_node_load[nid];
  2154. target_node = nid;
  2155. }
  2156. /* do some balance if several nodes have the same hit record */
  2157. if (target_node <= last_khugepaged_target_node)
  2158. for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
  2159. nid++)
  2160. if (max_value == khugepaged_node_load[nid]) {
  2161. target_node = nid;
  2162. break;
  2163. }
  2164. last_khugepaged_target_node = target_node;
  2165. return target_node;
  2166. }
  2167. static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
  2168. {
  2169. if (IS_ERR(*hpage)) {
  2170. if (!*wait)
  2171. return false;
  2172. *wait = false;
  2173. *hpage = NULL;
  2174. khugepaged_alloc_sleep();
  2175. } else if (*hpage) {
  2176. put_page(*hpage);
  2177. *hpage = NULL;
  2178. }
  2179. return true;
  2180. }
  2181. static struct page *
  2182. khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
  2183. unsigned long address, int node)
  2184. {
  2185. VM_BUG_ON_PAGE(*hpage, *hpage);
  2186. /*
  2187. * Before allocating the hugepage, release the mmap_sem read lock.
  2188. * The allocation can take potentially a long time if it involves
  2189. * sync compaction, and we do not need to hold the mmap_sem during
  2190. * that. We will recheck the vma after taking it again in write mode.
  2191. */
  2192. up_read(&mm->mmap_sem);
  2193. *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
  2194. if (unlikely(!*hpage)) {
  2195. count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
  2196. *hpage = ERR_PTR(-ENOMEM);
  2197. return NULL;
  2198. }
  2199. count_vm_event(THP_COLLAPSE_ALLOC);
  2200. return *hpage;
  2201. }
  2202. #else
  2203. static int khugepaged_find_target_node(void)
  2204. {
  2205. return 0;
  2206. }
  2207. static inline struct page *alloc_hugepage(int defrag)
  2208. {
  2209. return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
  2210. HPAGE_PMD_ORDER);
  2211. }
  2212. static struct page *khugepaged_alloc_hugepage(bool *wait)
  2213. {
  2214. struct page *hpage;
  2215. do {
  2216. hpage = alloc_hugepage(khugepaged_defrag());
  2217. if (!hpage) {
  2218. count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
  2219. if (!*wait)
  2220. return NULL;
  2221. *wait = false;
  2222. khugepaged_alloc_sleep();
  2223. } else
  2224. count_vm_event(THP_COLLAPSE_ALLOC);
  2225. } while (unlikely(!hpage) && likely(khugepaged_enabled()));
  2226. return hpage;
  2227. }
  2228. static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
  2229. {
  2230. if (!*hpage)
  2231. *hpage = khugepaged_alloc_hugepage(wait);
  2232. if (unlikely(!*hpage))
  2233. return false;
  2234. return true;
  2235. }
  2236. static struct page *
  2237. khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
  2238. unsigned long address, int node)
  2239. {
  2240. up_read(&mm->mmap_sem);
  2241. VM_BUG_ON(!*hpage);
  2242. return *hpage;
  2243. }
  2244. #endif
  2245. static bool hugepage_vma_check(struct vm_area_struct *vma)
  2246. {
  2247. if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
  2248. (vma->vm_flags & VM_NOHUGEPAGE))
  2249. return false;
  2250. if (!vma->anon_vma || vma->vm_ops)
  2251. return false;
  2252. if (is_vma_temporary_stack(vma))
  2253. return false;
  2254. return !(vma->vm_flags & VM_NO_THP);
  2255. }
  2256. static void collapse_huge_page(struct mm_struct *mm,
  2257. unsigned long address,
  2258. struct page **hpage,
  2259. struct vm_area_struct *vma,
  2260. int node)
  2261. {
  2262. pmd_t *pmd, _pmd;
  2263. pte_t *pte;
  2264. pgtable_t pgtable;
  2265. struct page *new_page;
  2266. spinlock_t *pmd_ptl, *pte_ptl;
  2267. int isolated;
  2268. unsigned long hstart, hend;
  2269. struct mem_cgroup *memcg;
  2270. unsigned long mmun_start; /* For mmu_notifiers */
  2271. unsigned long mmun_end; /* For mmu_notifiers */
  2272. gfp_t gfp;
  2273. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  2274. /* Only allocate from the target node */
  2275. gfp = alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE) |
  2276. __GFP_THISNODE;
  2277. /* release the mmap_sem read lock. */
  2278. new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
  2279. if (!new_page)
  2280. return;
  2281. if (unlikely(mem_cgroup_try_charge(new_page, mm,
  2282. gfp, &memcg)))
  2283. return;
  2284. /*
  2285. * Prevent all access to pagetables with the exception of
  2286. * gup_fast later hanlded by the ptep_clear_flush and the VM
  2287. * handled by the anon_vma lock + PG_lock.
  2288. */
  2289. down_write(&mm->mmap_sem);
  2290. if (unlikely(khugepaged_test_exit(mm)))
  2291. goto out;
  2292. vma = find_vma(mm, address);
  2293. if (!vma)
  2294. goto out;
  2295. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  2296. hend = vma->vm_end & HPAGE_PMD_MASK;
  2297. if (address < hstart || address + HPAGE_PMD_SIZE > hend)
  2298. goto out;
  2299. if (!hugepage_vma_check(vma))
  2300. goto out;
  2301. pmd = mm_find_pmd(mm, address);
  2302. if (!pmd)
  2303. goto out;
  2304. anon_vma_lock_write(vma->anon_vma);
  2305. pte = pte_offset_map(pmd, address);
  2306. pte_ptl = pte_lockptr(mm, pmd);
  2307. mmun_start = address;
  2308. mmun_end = address + HPAGE_PMD_SIZE;
  2309. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2310. pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
  2311. /*
  2312. * After this gup_fast can't run anymore. This also removes
  2313. * any huge TLB entry from the CPU so we won't allow
  2314. * huge and small TLB entries for the same virtual address
  2315. * to avoid the risk of CPU bugs in that area.
  2316. */
  2317. _pmd = pmdp_collapse_flush(vma, address, pmd);
  2318. spin_unlock(pmd_ptl);
  2319. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2320. spin_lock(pte_ptl);
  2321. isolated = __collapse_huge_page_isolate(vma, address, pte);
  2322. spin_unlock(pte_ptl);
  2323. if (unlikely(!isolated)) {
  2324. pte_unmap(pte);
  2325. spin_lock(pmd_ptl);
  2326. BUG_ON(!pmd_none(*pmd));
  2327. /*
  2328. * We can only use set_pmd_at when establishing
  2329. * hugepmds and never for establishing regular pmds that
  2330. * points to regular pagetables. Use pmd_populate for that
  2331. */
  2332. pmd_populate(mm, pmd, pmd_pgtable(_pmd));
  2333. spin_unlock(pmd_ptl);
  2334. anon_vma_unlock_write(vma->anon_vma);
  2335. goto out;
  2336. }
  2337. /*
  2338. * All pages are isolated and locked so anon_vma rmap
  2339. * can't run anymore.
  2340. */
  2341. anon_vma_unlock_write(vma->anon_vma);
  2342. __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
  2343. pte_unmap(pte);
  2344. __SetPageUptodate(new_page);
  2345. pgtable = pmd_pgtable(_pmd);
  2346. _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
  2347. _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
  2348. /*
  2349. * spin_lock() below is not the equivalent of smp_wmb(), so
  2350. * this is needed to avoid the copy_huge_page writes to become
  2351. * visible after the set_pmd_at() write.
  2352. */
  2353. smp_wmb();
  2354. spin_lock(pmd_ptl);
  2355. BUG_ON(!pmd_none(*pmd));
  2356. page_add_new_anon_rmap(new_page, vma, address);
  2357. mem_cgroup_commit_charge(new_page, memcg, false);
  2358. lru_cache_add_active_or_unevictable(new_page, vma);
  2359. pgtable_trans_huge_deposit(mm, pmd, pgtable);
  2360. set_pmd_at(mm, address, pmd, _pmd);
  2361. update_mmu_cache_pmd(vma, address, pmd);
  2362. spin_unlock(pmd_ptl);
  2363. *hpage = NULL;
  2364. khugepaged_pages_collapsed++;
  2365. out_up_write:
  2366. up_write(&mm->mmap_sem);
  2367. return;
  2368. out:
  2369. mem_cgroup_cancel_charge(new_page, memcg);
  2370. goto out_up_write;
  2371. }
  2372. static int khugepaged_scan_pmd(struct mm_struct *mm,
  2373. struct vm_area_struct *vma,
  2374. unsigned long address,
  2375. struct page **hpage)
  2376. {
  2377. pmd_t *pmd;
  2378. pte_t *pte, *_pte;
  2379. int ret = 0, none_or_zero = 0;
  2380. struct page *page;
  2381. unsigned long _address;
  2382. spinlock_t *ptl;
  2383. int node = NUMA_NO_NODE;
  2384. bool writable = false, referenced = false;
  2385. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  2386. pmd = mm_find_pmd(mm, address);
  2387. if (!pmd)
  2388. goto out;
  2389. memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
  2390. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  2391. for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
  2392. _pte++, _address += PAGE_SIZE) {
  2393. pte_t pteval = *_pte;
  2394. if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
  2395. if (!userfaultfd_armed(vma) &&
  2396. ++none_or_zero <= khugepaged_max_ptes_none)
  2397. continue;
  2398. else
  2399. goto out_unmap;
  2400. }
  2401. if (!pte_present(pteval))
  2402. goto out_unmap;
  2403. if (pte_write(pteval))
  2404. writable = true;
  2405. page = vm_normal_page(vma, _address, pteval);
  2406. if (unlikely(!page))
  2407. goto out_unmap;
  2408. /*
  2409. * Record which node the original page is from and save this
  2410. * information to khugepaged_node_load[].
  2411. * Khupaged will allocate hugepage from the node has the max
  2412. * hit record.
  2413. */
  2414. node = page_to_nid(page);
  2415. if (khugepaged_scan_abort(node))
  2416. goto out_unmap;
  2417. khugepaged_node_load[node]++;
  2418. VM_BUG_ON_PAGE(PageCompound(page), page);
  2419. if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
  2420. goto out_unmap;
  2421. /*
  2422. * cannot use mapcount: can't collapse if there's a gup pin.
  2423. * The page must only be referenced by the scanned process
  2424. * and page swap cache.
  2425. */
  2426. if (page_count(page) != 1 + !!PageSwapCache(page))
  2427. goto out_unmap;
  2428. if (pte_young(pteval) ||
  2429. page_is_young(page) || PageReferenced(page) ||
  2430. mmu_notifier_test_young(vma->vm_mm, address))
  2431. referenced = true;
  2432. }
  2433. if (referenced && writable)
  2434. ret = 1;
  2435. out_unmap:
  2436. pte_unmap_unlock(pte, ptl);
  2437. if (ret) {
  2438. node = khugepaged_find_target_node();
  2439. /* collapse_huge_page will return with the mmap_sem released */
  2440. collapse_huge_page(mm, address, hpage, vma, node);
  2441. }
  2442. out:
  2443. return ret;
  2444. }
  2445. static void collect_mm_slot(struct mm_slot *mm_slot)
  2446. {
  2447. struct mm_struct *mm = mm_slot->mm;
  2448. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
  2449. if (khugepaged_test_exit(mm)) {
  2450. /* free mm_slot */
  2451. hash_del(&mm_slot->hash);
  2452. list_del(&mm_slot->mm_node);
  2453. /*
  2454. * Not strictly needed because the mm exited already.
  2455. *
  2456. * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
  2457. */
  2458. /* khugepaged_mm_lock actually not necessary for the below */
  2459. free_mm_slot(mm_slot);
  2460. mmdrop(mm);
  2461. }
  2462. }
  2463. static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
  2464. struct page **hpage)
  2465. __releases(&khugepaged_mm_lock)
  2466. __acquires(&khugepaged_mm_lock)
  2467. {
  2468. struct mm_slot *mm_slot;
  2469. struct mm_struct *mm;
  2470. struct vm_area_struct *vma;
  2471. int progress = 0;
  2472. VM_BUG_ON(!pages);
  2473. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
  2474. if (khugepaged_scan.mm_slot)
  2475. mm_slot = khugepaged_scan.mm_slot;
  2476. else {
  2477. mm_slot = list_entry(khugepaged_scan.mm_head.next,
  2478. struct mm_slot, mm_node);
  2479. khugepaged_scan.address = 0;
  2480. khugepaged_scan.mm_slot = mm_slot;
  2481. }
  2482. spin_unlock(&khugepaged_mm_lock);
  2483. mm = mm_slot->mm;
  2484. down_read(&mm->mmap_sem);
  2485. if (unlikely(khugepaged_test_exit(mm)))
  2486. vma = NULL;
  2487. else
  2488. vma = find_vma(mm, khugepaged_scan.address);
  2489. progress++;
  2490. for (; vma; vma = vma->vm_next) {
  2491. unsigned long hstart, hend;
  2492. cond_resched();
  2493. if (unlikely(khugepaged_test_exit(mm))) {
  2494. progress++;
  2495. break;
  2496. }
  2497. if (!hugepage_vma_check(vma)) {
  2498. skip:
  2499. progress++;
  2500. continue;
  2501. }
  2502. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  2503. hend = vma->vm_end & HPAGE_PMD_MASK;
  2504. if (hstart >= hend)
  2505. goto skip;
  2506. if (khugepaged_scan.address > hend)
  2507. goto skip;
  2508. if (khugepaged_scan.address < hstart)
  2509. khugepaged_scan.address = hstart;
  2510. VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
  2511. while (khugepaged_scan.address < hend) {
  2512. int ret;
  2513. cond_resched();
  2514. if (unlikely(khugepaged_test_exit(mm)))
  2515. goto breakouterloop;
  2516. VM_BUG_ON(khugepaged_scan.address < hstart ||
  2517. khugepaged_scan.address + HPAGE_PMD_SIZE >
  2518. hend);
  2519. ret = khugepaged_scan_pmd(mm, vma,
  2520. khugepaged_scan.address,
  2521. hpage);
  2522. /* move to next address */
  2523. khugepaged_scan.address += HPAGE_PMD_SIZE;
  2524. progress += HPAGE_PMD_NR;
  2525. if (ret)
  2526. /* we released mmap_sem so break loop */
  2527. goto breakouterloop_mmap_sem;
  2528. if (progress >= pages)
  2529. goto breakouterloop;
  2530. }
  2531. }
  2532. breakouterloop:
  2533. up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
  2534. breakouterloop_mmap_sem:
  2535. spin_lock(&khugepaged_mm_lock);
  2536. VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
  2537. /*
  2538. * Release the current mm_slot if this mm is about to die, or
  2539. * if we scanned all vmas of this mm.
  2540. */
  2541. if (khugepaged_test_exit(mm) || !vma) {
  2542. /*
  2543. * Make sure that if mm_users is reaching zero while
  2544. * khugepaged runs here, khugepaged_exit will find
  2545. * mm_slot not pointing to the exiting mm.
  2546. */
  2547. if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
  2548. khugepaged_scan.mm_slot = list_entry(
  2549. mm_slot->mm_node.next,
  2550. struct mm_slot, mm_node);
  2551. khugepaged_scan.address = 0;
  2552. } else {
  2553. khugepaged_scan.mm_slot = NULL;
  2554. khugepaged_full_scans++;
  2555. }
  2556. collect_mm_slot(mm_slot);
  2557. }
  2558. return progress;
  2559. }
  2560. static int khugepaged_has_work(void)
  2561. {
  2562. return !list_empty(&khugepaged_scan.mm_head) &&
  2563. khugepaged_enabled();
  2564. }
  2565. static int khugepaged_wait_event(void)
  2566. {
  2567. return !list_empty(&khugepaged_scan.mm_head) ||
  2568. kthread_should_stop();
  2569. }
  2570. static void khugepaged_do_scan(void)
  2571. {
  2572. struct page *hpage = NULL;
  2573. unsigned int progress = 0, pass_through_head = 0;
  2574. unsigned int pages = khugepaged_pages_to_scan;
  2575. bool wait = true;
  2576. barrier(); /* write khugepaged_pages_to_scan to local stack */
  2577. while (progress < pages) {
  2578. if (!khugepaged_prealloc_page(&hpage, &wait))
  2579. break;
  2580. cond_resched();
  2581. if (unlikely(kthread_should_stop() || try_to_freeze()))
  2582. break;
  2583. spin_lock(&khugepaged_mm_lock);
  2584. if (!khugepaged_scan.mm_slot)
  2585. pass_through_head++;
  2586. if (khugepaged_has_work() &&
  2587. pass_through_head < 2)
  2588. progress += khugepaged_scan_mm_slot(pages - progress,
  2589. &hpage);
  2590. else
  2591. progress = pages;
  2592. spin_unlock(&khugepaged_mm_lock);
  2593. }
  2594. if (!IS_ERR_OR_NULL(hpage))
  2595. put_page(hpage);
  2596. }
  2597. static void khugepaged_wait_work(void)
  2598. {
  2599. if (khugepaged_has_work()) {
  2600. if (!khugepaged_scan_sleep_millisecs)
  2601. return;
  2602. wait_event_freezable_timeout(khugepaged_wait,
  2603. kthread_should_stop(),
  2604. msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
  2605. return;
  2606. }
  2607. if (khugepaged_enabled())
  2608. wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
  2609. }
  2610. static int khugepaged(void *none)
  2611. {
  2612. struct mm_slot *mm_slot;
  2613. set_freezable();
  2614. set_user_nice(current, MAX_NICE);
  2615. while (!kthread_should_stop()) {
  2616. khugepaged_do_scan();
  2617. khugepaged_wait_work();
  2618. }
  2619. spin_lock(&khugepaged_mm_lock);
  2620. mm_slot = khugepaged_scan.mm_slot;
  2621. khugepaged_scan.mm_slot = NULL;
  2622. if (mm_slot)
  2623. collect_mm_slot(mm_slot);
  2624. spin_unlock(&khugepaged_mm_lock);
  2625. return 0;
  2626. }
  2627. static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
  2628. unsigned long haddr, pmd_t *pmd)
  2629. {
  2630. struct mm_struct *mm = vma->vm_mm;
  2631. pgtable_t pgtable;
  2632. pmd_t _pmd;
  2633. int i;
  2634. pmdp_huge_clear_flush_notify(vma, haddr, pmd);
  2635. /* leave pmd empty until pte is filled */
  2636. pgtable = pgtable_trans_huge_withdraw(mm, pmd);
  2637. pmd_populate(mm, &_pmd, pgtable);
  2638. for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
  2639. pte_t *pte, entry;
  2640. entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
  2641. entry = pte_mkspecial(entry);
  2642. pte = pte_offset_map(&_pmd, haddr);
  2643. VM_BUG_ON(!pte_none(*pte));
  2644. set_pte_at(mm, haddr, pte, entry);
  2645. pte_unmap(pte);
  2646. }
  2647. smp_wmb(); /* make pte visible before pmd */
  2648. pmd_populate(mm, pmd, pgtable);
  2649. put_huge_zero_page();
  2650. }
  2651. void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
  2652. pmd_t *pmd)
  2653. {
  2654. spinlock_t *ptl;
  2655. struct page *page = NULL;
  2656. struct mm_struct *mm = vma->vm_mm;
  2657. unsigned long haddr = address & HPAGE_PMD_MASK;
  2658. unsigned long mmun_start; /* For mmu_notifiers */
  2659. unsigned long mmun_end; /* For mmu_notifiers */
  2660. BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
  2661. mmun_start = haddr;
  2662. mmun_end = haddr + HPAGE_PMD_SIZE;
  2663. again:
  2664. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2665. ptl = pmd_lock(mm, pmd);
  2666. if (unlikely(!pmd_trans_huge(*pmd)))
  2667. goto unlock;
  2668. if (vma_is_dax(vma)) {
  2669. pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
  2670. if (is_huge_zero_pmd(_pmd))
  2671. put_huge_zero_page();
  2672. } else if (is_huge_zero_pmd(*pmd)) {
  2673. __split_huge_zero_page_pmd(vma, haddr, pmd);
  2674. } else {
  2675. page = pmd_page(*pmd);
  2676. VM_BUG_ON_PAGE(!page_count(page), page);
  2677. get_page(page);
  2678. }
  2679. unlock:
  2680. spin_unlock(ptl);
  2681. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2682. if (!page)
  2683. return;
  2684. split_huge_page(page);
  2685. put_page(page);
  2686. /*
  2687. * We don't always have down_write of mmap_sem here: a racing
  2688. * do_huge_pmd_wp_page() might have copied-on-write to another
  2689. * huge page before our split_huge_page() got the anon_vma lock.
  2690. */
  2691. if (unlikely(pmd_trans_huge(*pmd)))
  2692. goto again;
  2693. }
  2694. void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
  2695. pmd_t *pmd)
  2696. {
  2697. struct vm_area_struct *vma;
  2698. vma = find_vma(mm, address);
  2699. BUG_ON(vma == NULL);
  2700. split_huge_page_pmd(vma, address, pmd);
  2701. }
  2702. static void split_huge_page_address(struct mm_struct *mm,
  2703. unsigned long address)
  2704. {
  2705. pgd_t *pgd;
  2706. pud_t *pud;
  2707. pmd_t *pmd;
  2708. VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
  2709. pgd = pgd_offset(mm, address);
  2710. if (!pgd_present(*pgd))
  2711. return;
  2712. pud = pud_offset(pgd, address);
  2713. if (!pud_present(*pud))
  2714. return;
  2715. pmd = pmd_offset(pud, address);
  2716. if (!pmd_present(*pmd))
  2717. return;
  2718. /*
  2719. * Caller holds the mmap_sem write mode, so a huge pmd cannot
  2720. * materialize from under us.
  2721. */
  2722. split_huge_page_pmd_mm(mm, address, pmd);
  2723. }
  2724. void vma_adjust_trans_huge(struct vm_area_struct *vma,
  2725. unsigned long start,
  2726. unsigned long end,
  2727. long adjust_next)
  2728. {
  2729. /*
  2730. * If the new start address isn't hpage aligned and it could
  2731. * previously contain an hugepage: check if we need to split
  2732. * an huge pmd.
  2733. */
  2734. if (start & ~HPAGE_PMD_MASK &&
  2735. (start & HPAGE_PMD_MASK) >= vma->vm_start &&
  2736. (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  2737. split_huge_page_address(vma->vm_mm, start);
  2738. /*
  2739. * If the new end address isn't hpage aligned and it could
  2740. * previously contain an hugepage: check if we need to split
  2741. * an huge pmd.
  2742. */
  2743. if (end & ~HPAGE_PMD_MASK &&
  2744. (end & HPAGE_PMD_MASK) >= vma->vm_start &&
  2745. (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  2746. split_huge_page_address(vma->vm_mm, end);
  2747. /*
  2748. * If we're also updating the vma->vm_next->vm_start, if the new
  2749. * vm_next->vm_start isn't page aligned and it could previously
  2750. * contain an hugepage: check if we need to split an huge pmd.
  2751. */
  2752. if (adjust_next > 0) {
  2753. struct vm_area_struct *next = vma->vm_next;
  2754. unsigned long nstart = next->vm_start;
  2755. nstart += adjust_next << PAGE_SHIFT;
  2756. if (nstart & ~HPAGE_PMD_MASK &&
  2757. (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
  2758. (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
  2759. split_huge_page_address(next->vm_mm, nstart);
  2760. }
  2761. }