swapfile.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750
  1. /*
  2. * linux/mm/swapfile.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. * Swap reorganised 29.12.95, Stephen Tweedie
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/sched/mm.h>
  9. #include <linux/sched/task.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/mman.h>
  12. #include <linux/slab.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/swap.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/namei.h>
  18. #include <linux/shmem_fs.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/random.h>
  21. #include <linux/writeback.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/init.h>
  25. #include <linux/ksm.h>
  26. #include <linux/rmap.h>
  27. #include <linux/security.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/mutex.h>
  30. #include <linux/capability.h>
  31. #include <linux/syscalls.h>
  32. #include <linux/memcontrol.h>
  33. #include <linux/poll.h>
  34. #include <linux/oom.h>
  35. #include <linux/frontswap.h>
  36. #include <linux/swapfile.h>
  37. #include <linux/export.h>
  38. #include <linux/swap_slots.h>
  39. #include <linux/sort.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/tlbflush.h>
  42. #include <linux/swapops.h>
  43. #include <linux/swap_cgroup.h>
  44. static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
  45. unsigned char);
  46. static void free_swap_count_continuations(struct swap_info_struct *);
  47. static sector_t map_swap_entry(swp_entry_t, struct block_device**);
  48. DEFINE_SPINLOCK(swap_lock);
  49. static unsigned int nr_swapfiles;
  50. atomic_long_t nr_swap_pages;
  51. /*
  52. * Some modules use swappable objects and may try to swap them out under
  53. * memory pressure (via the shrinker). Before doing so, they may wish to
  54. * check to see if any swap space is available.
  55. */
  56. EXPORT_SYMBOL_GPL(nr_swap_pages);
  57. /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
  58. long total_swap_pages;
  59. static int least_priority = -1;
  60. static const char Bad_file[] = "Bad swap file entry ";
  61. static const char Unused_file[] = "Unused swap file entry ";
  62. static const char Bad_offset[] = "Bad swap offset entry ";
  63. static const char Unused_offset[] = "Unused swap offset entry ";
  64. /*
  65. * all active swap_info_structs
  66. * protected with swap_lock, and ordered by priority.
  67. */
  68. PLIST_HEAD(swap_active_head);
  69. /*
  70. * all available (active, not full) swap_info_structs
  71. * protected with swap_avail_lock, ordered by priority.
  72. * This is used by get_swap_page() instead of swap_active_head
  73. * because swap_active_head includes all swap_info_structs,
  74. * but get_swap_page() doesn't need to look at full ones.
  75. * This uses its own lock instead of swap_lock because when a
  76. * swap_info_struct changes between not-full/full, it needs to
  77. * add/remove itself to/from this list, but the swap_info_struct->lock
  78. * is held and the locking order requires swap_lock to be taken
  79. * before any swap_info_struct->lock.
  80. */
  81. static struct plist_head *swap_avail_heads;
  82. static DEFINE_SPINLOCK(swap_avail_lock);
  83. struct swap_info_struct *swap_info[MAX_SWAPFILES];
  84. static DEFINE_MUTEX(swapon_mutex);
  85. static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
  86. /* Activity counter to indicate that a swapon or swapoff has occurred */
  87. static atomic_t proc_poll_event = ATOMIC_INIT(0);
  88. atomic_t nr_rotate_swap = ATOMIC_INIT(0);
  89. static inline unsigned char swap_count(unsigned char ent)
  90. {
  91. return ent & ~SWAP_HAS_CACHE; /* may include COUNT_CONTINUED flag */
  92. }
  93. /* returns 1 if swap entry is freed */
  94. static int
  95. __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
  96. {
  97. swp_entry_t entry = swp_entry(si->type, offset);
  98. struct page *page;
  99. int ret = 0;
  100. page = find_get_page(swap_address_space(entry), swp_offset(entry));
  101. if (!page)
  102. return 0;
  103. /*
  104. * This function is called from scan_swap_map() and it's called
  105. * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
  106. * We have to use trylock for avoiding deadlock. This is a special
  107. * case and you should use try_to_free_swap() with explicit lock_page()
  108. * in usual operations.
  109. */
  110. if (trylock_page(page)) {
  111. ret = try_to_free_swap(page);
  112. unlock_page(page);
  113. }
  114. put_page(page);
  115. return ret;
  116. }
  117. /*
  118. * swapon tell device that all the old swap contents can be discarded,
  119. * to allow the swap device to optimize its wear-levelling.
  120. */
  121. static int discard_swap(struct swap_info_struct *si)
  122. {
  123. struct swap_extent *se;
  124. sector_t start_block;
  125. sector_t nr_blocks;
  126. int err = 0;
  127. /* Do not discard the swap header page! */
  128. se = &si->first_swap_extent;
  129. start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
  130. nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
  131. if (nr_blocks) {
  132. err = blkdev_issue_discard(si->bdev, start_block,
  133. nr_blocks, GFP_KERNEL, 0);
  134. if (err)
  135. return err;
  136. cond_resched();
  137. }
  138. list_for_each_entry(se, &si->first_swap_extent.list, list) {
  139. start_block = se->start_block << (PAGE_SHIFT - 9);
  140. nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
  141. err = blkdev_issue_discard(si->bdev, start_block,
  142. nr_blocks, GFP_KERNEL, 0);
  143. if (err)
  144. break;
  145. cond_resched();
  146. }
  147. return err; /* That will often be -EOPNOTSUPP */
  148. }
  149. /*
  150. * swap allocation tell device that a cluster of swap can now be discarded,
  151. * to allow the swap device to optimize its wear-levelling.
  152. */
  153. static void discard_swap_cluster(struct swap_info_struct *si,
  154. pgoff_t start_page, pgoff_t nr_pages)
  155. {
  156. struct swap_extent *se = si->curr_swap_extent;
  157. int found_extent = 0;
  158. while (nr_pages) {
  159. if (se->start_page <= start_page &&
  160. start_page < se->start_page + se->nr_pages) {
  161. pgoff_t offset = start_page - se->start_page;
  162. sector_t start_block = se->start_block + offset;
  163. sector_t nr_blocks = se->nr_pages - offset;
  164. if (nr_blocks > nr_pages)
  165. nr_blocks = nr_pages;
  166. start_page += nr_blocks;
  167. nr_pages -= nr_blocks;
  168. if (!found_extent++)
  169. si->curr_swap_extent = se;
  170. start_block <<= PAGE_SHIFT - 9;
  171. nr_blocks <<= PAGE_SHIFT - 9;
  172. if (blkdev_issue_discard(si->bdev, start_block,
  173. nr_blocks, GFP_NOIO, 0))
  174. break;
  175. }
  176. se = list_next_entry(se, list);
  177. }
  178. }
  179. #ifdef CONFIG_THP_SWAP
  180. #define SWAPFILE_CLUSTER HPAGE_PMD_NR
  181. #else
  182. #define SWAPFILE_CLUSTER 256
  183. #endif
  184. #define LATENCY_LIMIT 256
  185. static inline void cluster_set_flag(struct swap_cluster_info *info,
  186. unsigned int flag)
  187. {
  188. info->flags = flag;
  189. }
  190. static inline unsigned int cluster_count(struct swap_cluster_info *info)
  191. {
  192. return info->data;
  193. }
  194. static inline void cluster_set_count(struct swap_cluster_info *info,
  195. unsigned int c)
  196. {
  197. info->data = c;
  198. }
  199. static inline void cluster_set_count_flag(struct swap_cluster_info *info,
  200. unsigned int c, unsigned int f)
  201. {
  202. info->flags = f;
  203. info->data = c;
  204. }
  205. static inline unsigned int cluster_next(struct swap_cluster_info *info)
  206. {
  207. return info->data;
  208. }
  209. static inline void cluster_set_next(struct swap_cluster_info *info,
  210. unsigned int n)
  211. {
  212. info->data = n;
  213. }
  214. static inline void cluster_set_next_flag(struct swap_cluster_info *info,
  215. unsigned int n, unsigned int f)
  216. {
  217. info->flags = f;
  218. info->data = n;
  219. }
  220. static inline bool cluster_is_free(struct swap_cluster_info *info)
  221. {
  222. return info->flags & CLUSTER_FLAG_FREE;
  223. }
  224. static inline bool cluster_is_null(struct swap_cluster_info *info)
  225. {
  226. return info->flags & CLUSTER_FLAG_NEXT_NULL;
  227. }
  228. static inline void cluster_set_null(struct swap_cluster_info *info)
  229. {
  230. info->flags = CLUSTER_FLAG_NEXT_NULL;
  231. info->data = 0;
  232. }
  233. static inline bool cluster_is_huge(struct swap_cluster_info *info)
  234. {
  235. return info->flags & CLUSTER_FLAG_HUGE;
  236. }
  237. static inline void cluster_clear_huge(struct swap_cluster_info *info)
  238. {
  239. info->flags &= ~CLUSTER_FLAG_HUGE;
  240. }
  241. static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si,
  242. unsigned long offset)
  243. {
  244. struct swap_cluster_info *ci;
  245. ci = si->cluster_info;
  246. if (ci) {
  247. ci += offset / SWAPFILE_CLUSTER;
  248. spin_lock(&ci->lock);
  249. }
  250. return ci;
  251. }
  252. static inline void unlock_cluster(struct swap_cluster_info *ci)
  253. {
  254. if (ci)
  255. spin_unlock(&ci->lock);
  256. }
  257. static inline struct swap_cluster_info *lock_cluster_or_swap_info(
  258. struct swap_info_struct *si,
  259. unsigned long offset)
  260. {
  261. struct swap_cluster_info *ci;
  262. ci = lock_cluster(si, offset);
  263. if (!ci)
  264. spin_lock(&si->lock);
  265. return ci;
  266. }
  267. static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si,
  268. struct swap_cluster_info *ci)
  269. {
  270. if (ci)
  271. unlock_cluster(ci);
  272. else
  273. spin_unlock(&si->lock);
  274. }
  275. static inline bool cluster_list_empty(struct swap_cluster_list *list)
  276. {
  277. return cluster_is_null(&list->head);
  278. }
  279. static inline unsigned int cluster_list_first(struct swap_cluster_list *list)
  280. {
  281. return cluster_next(&list->head);
  282. }
  283. static void cluster_list_init(struct swap_cluster_list *list)
  284. {
  285. cluster_set_null(&list->head);
  286. cluster_set_null(&list->tail);
  287. }
  288. static void cluster_list_add_tail(struct swap_cluster_list *list,
  289. struct swap_cluster_info *ci,
  290. unsigned int idx)
  291. {
  292. if (cluster_list_empty(list)) {
  293. cluster_set_next_flag(&list->head, idx, 0);
  294. cluster_set_next_flag(&list->tail, idx, 0);
  295. } else {
  296. struct swap_cluster_info *ci_tail;
  297. unsigned int tail = cluster_next(&list->tail);
  298. /*
  299. * Nested cluster lock, but both cluster locks are
  300. * only acquired when we held swap_info_struct->lock
  301. */
  302. ci_tail = ci + tail;
  303. spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING);
  304. cluster_set_next(ci_tail, idx);
  305. spin_unlock(&ci_tail->lock);
  306. cluster_set_next_flag(&list->tail, idx, 0);
  307. }
  308. }
  309. static unsigned int cluster_list_del_first(struct swap_cluster_list *list,
  310. struct swap_cluster_info *ci)
  311. {
  312. unsigned int idx;
  313. idx = cluster_next(&list->head);
  314. if (cluster_next(&list->tail) == idx) {
  315. cluster_set_null(&list->head);
  316. cluster_set_null(&list->tail);
  317. } else
  318. cluster_set_next_flag(&list->head,
  319. cluster_next(&ci[idx]), 0);
  320. return idx;
  321. }
  322. /* Add a cluster to discard list and schedule it to do discard */
  323. static void swap_cluster_schedule_discard(struct swap_info_struct *si,
  324. unsigned int idx)
  325. {
  326. /*
  327. * If scan_swap_map() can't find a free cluster, it will check
  328. * si->swap_map directly. To make sure the discarding cluster isn't
  329. * taken by scan_swap_map(), mark the swap entries bad (occupied). It
  330. * will be cleared after discard
  331. */
  332. memset(si->swap_map + idx * SWAPFILE_CLUSTER,
  333. SWAP_MAP_BAD, SWAPFILE_CLUSTER);
  334. cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx);
  335. schedule_work(&si->discard_work);
  336. }
  337. static void __free_cluster(struct swap_info_struct *si, unsigned long idx)
  338. {
  339. struct swap_cluster_info *ci = si->cluster_info;
  340. cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE);
  341. cluster_list_add_tail(&si->free_clusters, ci, idx);
  342. }
  343. /*
  344. * Doing discard actually. After a cluster discard is finished, the cluster
  345. * will be added to free cluster list. caller should hold si->lock.
  346. */
  347. static void swap_do_scheduled_discard(struct swap_info_struct *si)
  348. {
  349. struct swap_cluster_info *info, *ci;
  350. unsigned int idx;
  351. info = si->cluster_info;
  352. while (!cluster_list_empty(&si->discard_clusters)) {
  353. idx = cluster_list_del_first(&si->discard_clusters, info);
  354. spin_unlock(&si->lock);
  355. discard_swap_cluster(si, idx * SWAPFILE_CLUSTER,
  356. SWAPFILE_CLUSTER);
  357. spin_lock(&si->lock);
  358. ci = lock_cluster(si, idx * SWAPFILE_CLUSTER);
  359. __free_cluster(si, idx);
  360. memset(si->swap_map + idx * SWAPFILE_CLUSTER,
  361. 0, SWAPFILE_CLUSTER);
  362. unlock_cluster(ci);
  363. }
  364. }
  365. static void swap_discard_work(struct work_struct *work)
  366. {
  367. struct swap_info_struct *si;
  368. si = container_of(work, struct swap_info_struct, discard_work);
  369. spin_lock(&si->lock);
  370. swap_do_scheduled_discard(si);
  371. spin_unlock(&si->lock);
  372. }
  373. static void alloc_cluster(struct swap_info_struct *si, unsigned long idx)
  374. {
  375. struct swap_cluster_info *ci = si->cluster_info;
  376. VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx);
  377. cluster_list_del_first(&si->free_clusters, ci);
  378. cluster_set_count_flag(ci + idx, 0, 0);
  379. }
  380. static void free_cluster(struct swap_info_struct *si, unsigned long idx)
  381. {
  382. struct swap_cluster_info *ci = si->cluster_info + idx;
  383. VM_BUG_ON(cluster_count(ci) != 0);
  384. /*
  385. * If the swap is discardable, prepare discard the cluster
  386. * instead of free it immediately. The cluster will be freed
  387. * after discard.
  388. */
  389. if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) ==
  390. (SWP_WRITEOK | SWP_PAGE_DISCARD)) {
  391. swap_cluster_schedule_discard(si, idx);
  392. return;
  393. }
  394. __free_cluster(si, idx);
  395. }
  396. /*
  397. * The cluster corresponding to page_nr will be used. The cluster will be
  398. * removed from free cluster list and its usage counter will be increased.
  399. */
  400. static void inc_cluster_info_page(struct swap_info_struct *p,
  401. struct swap_cluster_info *cluster_info, unsigned long page_nr)
  402. {
  403. unsigned long idx = page_nr / SWAPFILE_CLUSTER;
  404. if (!cluster_info)
  405. return;
  406. if (cluster_is_free(&cluster_info[idx]))
  407. alloc_cluster(p, idx);
  408. VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER);
  409. cluster_set_count(&cluster_info[idx],
  410. cluster_count(&cluster_info[idx]) + 1);
  411. }
  412. /*
  413. * The cluster corresponding to page_nr decreases one usage. If the usage
  414. * counter becomes 0, which means no page in the cluster is in using, we can
  415. * optionally discard the cluster and add it to free cluster list.
  416. */
  417. static void dec_cluster_info_page(struct swap_info_struct *p,
  418. struct swap_cluster_info *cluster_info, unsigned long page_nr)
  419. {
  420. unsigned long idx = page_nr / SWAPFILE_CLUSTER;
  421. if (!cluster_info)
  422. return;
  423. VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0);
  424. cluster_set_count(&cluster_info[idx],
  425. cluster_count(&cluster_info[idx]) - 1);
  426. if (cluster_count(&cluster_info[idx]) == 0)
  427. free_cluster(p, idx);
  428. }
  429. /*
  430. * It's possible scan_swap_map() uses a free cluster in the middle of free
  431. * cluster list. Avoiding such abuse to avoid list corruption.
  432. */
  433. static bool
  434. scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si,
  435. unsigned long offset)
  436. {
  437. struct percpu_cluster *percpu_cluster;
  438. bool conflict;
  439. offset /= SWAPFILE_CLUSTER;
  440. conflict = !cluster_list_empty(&si->free_clusters) &&
  441. offset != cluster_list_first(&si->free_clusters) &&
  442. cluster_is_free(&si->cluster_info[offset]);
  443. if (!conflict)
  444. return false;
  445. percpu_cluster = this_cpu_ptr(si->percpu_cluster);
  446. cluster_set_null(&percpu_cluster->index);
  447. return true;
  448. }
  449. /*
  450. * Try to get a swap entry from current cpu's swap entry pool (a cluster). This
  451. * might involve allocating a new cluster for current CPU too.
  452. */
  453. static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
  454. unsigned long *offset, unsigned long *scan_base)
  455. {
  456. struct percpu_cluster *cluster;
  457. struct swap_cluster_info *ci;
  458. bool found_free;
  459. unsigned long tmp, max;
  460. new_cluster:
  461. cluster = this_cpu_ptr(si->percpu_cluster);
  462. if (cluster_is_null(&cluster->index)) {
  463. if (!cluster_list_empty(&si->free_clusters)) {
  464. cluster->index = si->free_clusters.head;
  465. cluster->next = cluster_next(&cluster->index) *
  466. SWAPFILE_CLUSTER;
  467. } else if (!cluster_list_empty(&si->discard_clusters)) {
  468. /*
  469. * we don't have free cluster but have some clusters in
  470. * discarding, do discard now and reclaim them
  471. */
  472. swap_do_scheduled_discard(si);
  473. *scan_base = *offset = si->cluster_next;
  474. goto new_cluster;
  475. } else
  476. return false;
  477. }
  478. found_free = false;
  479. /*
  480. * Other CPUs can use our cluster if they can't find a free cluster,
  481. * check if there is still free entry in the cluster
  482. */
  483. tmp = cluster->next;
  484. max = min_t(unsigned long, si->max,
  485. (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER);
  486. if (tmp >= max) {
  487. cluster_set_null(&cluster->index);
  488. goto new_cluster;
  489. }
  490. ci = lock_cluster(si, tmp);
  491. while (tmp < max) {
  492. if (!si->swap_map[tmp]) {
  493. found_free = true;
  494. break;
  495. }
  496. tmp++;
  497. }
  498. unlock_cluster(ci);
  499. if (!found_free) {
  500. cluster_set_null(&cluster->index);
  501. goto new_cluster;
  502. }
  503. cluster->next = tmp + 1;
  504. *offset = tmp;
  505. *scan_base = tmp;
  506. return found_free;
  507. }
  508. static void __del_from_avail_list(struct swap_info_struct *p)
  509. {
  510. int nid;
  511. for_each_node(nid)
  512. plist_del(&p->avail_lists[nid], &swap_avail_heads[nid]);
  513. }
  514. static void del_from_avail_list(struct swap_info_struct *p)
  515. {
  516. spin_lock(&swap_avail_lock);
  517. __del_from_avail_list(p);
  518. spin_unlock(&swap_avail_lock);
  519. }
  520. static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset,
  521. unsigned int nr_entries)
  522. {
  523. unsigned int end = offset + nr_entries - 1;
  524. if (offset == si->lowest_bit)
  525. si->lowest_bit += nr_entries;
  526. if (end == si->highest_bit)
  527. si->highest_bit -= nr_entries;
  528. si->inuse_pages += nr_entries;
  529. if (si->inuse_pages == si->pages) {
  530. si->lowest_bit = si->max;
  531. si->highest_bit = 0;
  532. del_from_avail_list(si);
  533. }
  534. }
  535. static void add_to_avail_list(struct swap_info_struct *p)
  536. {
  537. int nid;
  538. spin_lock(&swap_avail_lock);
  539. for_each_node(nid) {
  540. WARN_ON(!plist_node_empty(&p->avail_lists[nid]));
  541. plist_add(&p->avail_lists[nid], &swap_avail_heads[nid]);
  542. }
  543. spin_unlock(&swap_avail_lock);
  544. }
  545. static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
  546. unsigned int nr_entries)
  547. {
  548. unsigned long end = offset + nr_entries - 1;
  549. void (*swap_slot_free_notify)(struct block_device *, unsigned long);
  550. if (offset < si->lowest_bit)
  551. si->lowest_bit = offset;
  552. if (end > si->highest_bit) {
  553. bool was_full = !si->highest_bit;
  554. si->highest_bit = end;
  555. if (was_full && (si->flags & SWP_WRITEOK))
  556. add_to_avail_list(si);
  557. }
  558. atomic_long_add(nr_entries, &nr_swap_pages);
  559. si->inuse_pages -= nr_entries;
  560. if (si->flags & SWP_BLKDEV)
  561. swap_slot_free_notify =
  562. si->bdev->bd_disk->fops->swap_slot_free_notify;
  563. else
  564. swap_slot_free_notify = NULL;
  565. while (offset <= end) {
  566. frontswap_invalidate_page(si->type, offset);
  567. if (swap_slot_free_notify)
  568. swap_slot_free_notify(si->bdev, offset);
  569. offset++;
  570. }
  571. }
  572. static int scan_swap_map_slots(struct swap_info_struct *si,
  573. unsigned char usage, int nr,
  574. swp_entry_t slots[])
  575. {
  576. struct swap_cluster_info *ci;
  577. unsigned long offset;
  578. unsigned long scan_base;
  579. unsigned long last_in_cluster = 0;
  580. int latency_ration = LATENCY_LIMIT;
  581. int n_ret = 0;
  582. if (nr > SWAP_BATCH)
  583. nr = SWAP_BATCH;
  584. /*
  585. * We try to cluster swap pages by allocating them sequentially
  586. * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
  587. * way, however, we resort to first-free allocation, starting
  588. * a new cluster. This prevents us from scattering swap pages
  589. * all over the entire swap partition, so that we reduce
  590. * overall disk seek times between swap pages. -- sct
  591. * But we do now try to find an empty cluster. -Andrea
  592. * And we let swap pages go all over an SSD partition. Hugh
  593. */
  594. si->flags += SWP_SCANNING;
  595. scan_base = offset = si->cluster_next;
  596. /* SSD algorithm */
  597. if (si->cluster_info) {
  598. if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
  599. goto checks;
  600. else
  601. goto scan;
  602. }
  603. if (unlikely(!si->cluster_nr--)) {
  604. if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
  605. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  606. goto checks;
  607. }
  608. spin_unlock(&si->lock);
  609. /*
  610. * If seek is expensive, start searching for new cluster from
  611. * start of partition, to minimize the span of allocated swap.
  612. * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info
  613. * case, just handled by scan_swap_map_try_ssd_cluster() above.
  614. */
  615. scan_base = offset = si->lowest_bit;
  616. last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
  617. /* Locate the first empty (unaligned) cluster */
  618. for (; last_in_cluster <= si->highest_bit; offset++) {
  619. if (si->swap_map[offset])
  620. last_in_cluster = offset + SWAPFILE_CLUSTER;
  621. else if (offset == last_in_cluster) {
  622. spin_lock(&si->lock);
  623. offset -= SWAPFILE_CLUSTER - 1;
  624. si->cluster_next = offset;
  625. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  626. goto checks;
  627. }
  628. if (unlikely(--latency_ration < 0)) {
  629. cond_resched();
  630. latency_ration = LATENCY_LIMIT;
  631. }
  632. }
  633. offset = scan_base;
  634. spin_lock(&si->lock);
  635. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  636. }
  637. checks:
  638. if (si->cluster_info) {
  639. while (scan_swap_map_ssd_cluster_conflict(si, offset)) {
  640. /* take a break if we already got some slots */
  641. if (n_ret)
  642. goto done;
  643. if (!scan_swap_map_try_ssd_cluster(si, &offset,
  644. &scan_base))
  645. goto scan;
  646. }
  647. }
  648. if (!(si->flags & SWP_WRITEOK))
  649. goto no_page;
  650. if (!si->highest_bit)
  651. goto no_page;
  652. if (offset > si->highest_bit)
  653. scan_base = offset = si->lowest_bit;
  654. ci = lock_cluster(si, offset);
  655. /* reuse swap entry of cache-only swap if not busy. */
  656. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  657. int swap_was_freed;
  658. unlock_cluster(ci);
  659. spin_unlock(&si->lock);
  660. swap_was_freed = __try_to_reclaim_swap(si, offset);
  661. spin_lock(&si->lock);
  662. /* entry was freed successfully, try to use this again */
  663. if (swap_was_freed)
  664. goto checks;
  665. goto scan; /* check next one */
  666. }
  667. if (si->swap_map[offset]) {
  668. unlock_cluster(ci);
  669. if (!n_ret)
  670. goto scan;
  671. else
  672. goto done;
  673. }
  674. si->swap_map[offset] = usage;
  675. inc_cluster_info_page(si, si->cluster_info, offset);
  676. unlock_cluster(ci);
  677. swap_range_alloc(si, offset, 1);
  678. si->cluster_next = offset + 1;
  679. slots[n_ret++] = swp_entry(si->type, offset);
  680. /* got enough slots or reach max slots? */
  681. if ((n_ret == nr) || (offset >= si->highest_bit))
  682. goto done;
  683. /* search for next available slot */
  684. /* time to take a break? */
  685. if (unlikely(--latency_ration < 0)) {
  686. if (n_ret)
  687. goto done;
  688. spin_unlock(&si->lock);
  689. cond_resched();
  690. spin_lock(&si->lock);
  691. latency_ration = LATENCY_LIMIT;
  692. }
  693. /* try to get more slots in cluster */
  694. if (si->cluster_info) {
  695. if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
  696. goto checks;
  697. else
  698. goto done;
  699. }
  700. /* non-ssd case */
  701. ++offset;
  702. /* non-ssd case, still more slots in cluster? */
  703. if (si->cluster_nr && !si->swap_map[offset]) {
  704. --si->cluster_nr;
  705. goto checks;
  706. }
  707. done:
  708. si->flags -= SWP_SCANNING;
  709. return n_ret;
  710. scan:
  711. spin_unlock(&si->lock);
  712. while (++offset <= si->highest_bit) {
  713. if (!si->swap_map[offset]) {
  714. spin_lock(&si->lock);
  715. goto checks;
  716. }
  717. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  718. spin_lock(&si->lock);
  719. goto checks;
  720. }
  721. if (unlikely(--latency_ration < 0)) {
  722. cond_resched();
  723. latency_ration = LATENCY_LIMIT;
  724. }
  725. }
  726. offset = si->lowest_bit;
  727. while (offset < scan_base) {
  728. if (!si->swap_map[offset]) {
  729. spin_lock(&si->lock);
  730. goto checks;
  731. }
  732. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  733. spin_lock(&si->lock);
  734. goto checks;
  735. }
  736. if (unlikely(--latency_ration < 0)) {
  737. cond_resched();
  738. latency_ration = LATENCY_LIMIT;
  739. }
  740. offset++;
  741. }
  742. spin_lock(&si->lock);
  743. no_page:
  744. si->flags -= SWP_SCANNING;
  745. return n_ret;
  746. }
  747. #ifdef CONFIG_THP_SWAP
  748. static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot)
  749. {
  750. unsigned long idx;
  751. struct swap_cluster_info *ci;
  752. unsigned long offset, i;
  753. unsigned char *map;
  754. if (cluster_list_empty(&si->free_clusters))
  755. return 0;
  756. idx = cluster_list_first(&si->free_clusters);
  757. offset = idx * SWAPFILE_CLUSTER;
  758. ci = lock_cluster(si, offset);
  759. alloc_cluster(si, idx);
  760. cluster_set_count_flag(ci, SWAPFILE_CLUSTER, CLUSTER_FLAG_HUGE);
  761. map = si->swap_map + offset;
  762. for (i = 0; i < SWAPFILE_CLUSTER; i++)
  763. map[i] = SWAP_HAS_CACHE;
  764. unlock_cluster(ci);
  765. swap_range_alloc(si, offset, SWAPFILE_CLUSTER);
  766. *slot = swp_entry(si->type, offset);
  767. return 1;
  768. }
  769. static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx)
  770. {
  771. unsigned long offset = idx * SWAPFILE_CLUSTER;
  772. struct swap_cluster_info *ci;
  773. ci = lock_cluster(si, offset);
  774. cluster_set_count_flag(ci, 0, 0);
  775. free_cluster(si, idx);
  776. unlock_cluster(ci);
  777. swap_range_free(si, offset, SWAPFILE_CLUSTER);
  778. }
  779. #else
  780. static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot)
  781. {
  782. VM_WARN_ON_ONCE(1);
  783. return 0;
  784. }
  785. #endif /* CONFIG_THP_SWAP */
  786. static unsigned long scan_swap_map(struct swap_info_struct *si,
  787. unsigned char usage)
  788. {
  789. swp_entry_t entry;
  790. int n_ret;
  791. n_ret = scan_swap_map_slots(si, usage, 1, &entry);
  792. if (n_ret)
  793. return swp_offset(entry);
  794. else
  795. return 0;
  796. }
  797. int get_swap_pages(int n_goal, bool cluster, swp_entry_t swp_entries[])
  798. {
  799. unsigned long nr_pages = cluster ? SWAPFILE_CLUSTER : 1;
  800. struct swap_info_struct *si, *next;
  801. long avail_pgs;
  802. int n_ret = 0;
  803. int node;
  804. /* Only single cluster request supported */
  805. WARN_ON_ONCE(n_goal > 1 && cluster);
  806. avail_pgs = atomic_long_read(&nr_swap_pages) / nr_pages;
  807. if (avail_pgs <= 0)
  808. goto noswap;
  809. if (n_goal > SWAP_BATCH)
  810. n_goal = SWAP_BATCH;
  811. if (n_goal > avail_pgs)
  812. n_goal = avail_pgs;
  813. atomic_long_sub(n_goal * nr_pages, &nr_swap_pages);
  814. spin_lock(&swap_avail_lock);
  815. start_over:
  816. node = numa_node_id();
  817. plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) {
  818. /* requeue si to after same-priority siblings */
  819. plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]);
  820. spin_unlock(&swap_avail_lock);
  821. spin_lock(&si->lock);
  822. if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) {
  823. spin_lock(&swap_avail_lock);
  824. if (plist_node_empty(&si->avail_lists[node])) {
  825. spin_unlock(&si->lock);
  826. goto nextsi;
  827. }
  828. WARN(!si->highest_bit,
  829. "swap_info %d in list but !highest_bit\n",
  830. si->type);
  831. WARN(!(si->flags & SWP_WRITEOK),
  832. "swap_info %d in list but !SWP_WRITEOK\n",
  833. si->type);
  834. __del_from_avail_list(si);
  835. spin_unlock(&si->lock);
  836. goto nextsi;
  837. }
  838. if (cluster) {
  839. if (!(si->flags & SWP_FILE))
  840. n_ret = swap_alloc_cluster(si, swp_entries);
  841. } else
  842. n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE,
  843. n_goal, swp_entries);
  844. spin_unlock(&si->lock);
  845. if (n_ret || cluster)
  846. goto check_out;
  847. pr_debug("scan_swap_map of si %d failed to find offset\n",
  848. si->type);
  849. spin_lock(&swap_avail_lock);
  850. nextsi:
  851. /*
  852. * if we got here, it's likely that si was almost full before,
  853. * and since scan_swap_map() can drop the si->lock, multiple
  854. * callers probably all tried to get a page from the same si
  855. * and it filled up before we could get one; or, the si filled
  856. * up between us dropping swap_avail_lock and taking si->lock.
  857. * Since we dropped the swap_avail_lock, the swap_avail_head
  858. * list may have been modified; so if next is still in the
  859. * swap_avail_head list then try it, otherwise start over
  860. * if we have not gotten any slots.
  861. */
  862. if (plist_node_empty(&next->avail_lists[node]))
  863. goto start_over;
  864. }
  865. spin_unlock(&swap_avail_lock);
  866. check_out:
  867. if (n_ret < n_goal)
  868. atomic_long_add((long)(n_goal - n_ret) * nr_pages,
  869. &nr_swap_pages);
  870. noswap:
  871. return n_ret;
  872. }
  873. /* The only caller of this function is now suspend routine */
  874. swp_entry_t get_swap_page_of_type(int type)
  875. {
  876. struct swap_info_struct *si;
  877. pgoff_t offset;
  878. si = swap_info[type];
  879. spin_lock(&si->lock);
  880. if (si && (si->flags & SWP_WRITEOK)) {
  881. atomic_long_dec(&nr_swap_pages);
  882. /* This is called for allocating swap entry, not cache */
  883. offset = scan_swap_map(si, 1);
  884. if (offset) {
  885. spin_unlock(&si->lock);
  886. return swp_entry(type, offset);
  887. }
  888. atomic_long_inc(&nr_swap_pages);
  889. }
  890. spin_unlock(&si->lock);
  891. return (swp_entry_t) {0};
  892. }
  893. static struct swap_info_struct *__swap_info_get(swp_entry_t entry)
  894. {
  895. struct swap_info_struct *p;
  896. unsigned long offset, type;
  897. if (!entry.val)
  898. goto out;
  899. type = swp_type(entry);
  900. if (type >= nr_swapfiles)
  901. goto bad_nofile;
  902. p = swap_info[type];
  903. if (!(p->flags & SWP_USED))
  904. goto bad_device;
  905. offset = swp_offset(entry);
  906. if (offset >= p->max)
  907. goto bad_offset;
  908. return p;
  909. bad_offset:
  910. pr_err("swap_info_get: %s%08lx\n", Bad_offset, entry.val);
  911. goto out;
  912. bad_device:
  913. pr_err("swap_info_get: %s%08lx\n", Unused_file, entry.val);
  914. goto out;
  915. bad_nofile:
  916. pr_err("swap_info_get: %s%08lx\n", Bad_file, entry.val);
  917. out:
  918. return NULL;
  919. }
  920. static struct swap_info_struct *_swap_info_get(swp_entry_t entry)
  921. {
  922. struct swap_info_struct *p;
  923. p = __swap_info_get(entry);
  924. if (!p)
  925. goto out;
  926. if (!p->swap_map[swp_offset(entry)])
  927. goto bad_free;
  928. return p;
  929. bad_free:
  930. pr_err("swap_info_get: %s%08lx\n", Unused_offset, entry.val);
  931. goto out;
  932. out:
  933. return NULL;
  934. }
  935. static struct swap_info_struct *swap_info_get(swp_entry_t entry)
  936. {
  937. struct swap_info_struct *p;
  938. p = _swap_info_get(entry);
  939. if (p)
  940. spin_lock(&p->lock);
  941. return p;
  942. }
  943. static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
  944. struct swap_info_struct *q)
  945. {
  946. struct swap_info_struct *p;
  947. p = _swap_info_get(entry);
  948. if (p != q) {
  949. if (q != NULL)
  950. spin_unlock(&q->lock);
  951. if (p != NULL)
  952. spin_lock(&p->lock);
  953. }
  954. return p;
  955. }
  956. static unsigned char __swap_entry_free(struct swap_info_struct *p,
  957. swp_entry_t entry, unsigned char usage)
  958. {
  959. struct swap_cluster_info *ci;
  960. unsigned long offset = swp_offset(entry);
  961. unsigned char count;
  962. unsigned char has_cache;
  963. ci = lock_cluster_or_swap_info(p, offset);
  964. count = p->swap_map[offset];
  965. has_cache = count & SWAP_HAS_CACHE;
  966. count &= ~SWAP_HAS_CACHE;
  967. if (usage == SWAP_HAS_CACHE) {
  968. VM_BUG_ON(!has_cache);
  969. has_cache = 0;
  970. } else if (count == SWAP_MAP_SHMEM) {
  971. /*
  972. * Or we could insist on shmem.c using a special
  973. * swap_shmem_free() and free_shmem_swap_and_cache()...
  974. */
  975. count = 0;
  976. } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
  977. if (count == COUNT_CONTINUED) {
  978. if (swap_count_continued(p, offset, count))
  979. count = SWAP_MAP_MAX | COUNT_CONTINUED;
  980. else
  981. count = SWAP_MAP_MAX;
  982. } else
  983. count--;
  984. }
  985. usage = count | has_cache;
  986. p->swap_map[offset] = usage ? : SWAP_HAS_CACHE;
  987. unlock_cluster_or_swap_info(p, ci);
  988. return usage;
  989. }
  990. static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry)
  991. {
  992. struct swap_cluster_info *ci;
  993. unsigned long offset = swp_offset(entry);
  994. unsigned char count;
  995. ci = lock_cluster(p, offset);
  996. count = p->swap_map[offset];
  997. VM_BUG_ON(count != SWAP_HAS_CACHE);
  998. p->swap_map[offset] = 0;
  999. dec_cluster_info_page(p, p->cluster_info, offset);
  1000. unlock_cluster(ci);
  1001. mem_cgroup_uncharge_swap(entry, 1);
  1002. swap_range_free(p, offset, 1);
  1003. }
  1004. /*
  1005. * Caller has made sure that the swap device corresponding to entry
  1006. * is still around or has not been recycled.
  1007. */
  1008. void swap_free(swp_entry_t entry)
  1009. {
  1010. struct swap_info_struct *p;
  1011. p = _swap_info_get(entry);
  1012. if (p) {
  1013. if (!__swap_entry_free(p, entry, 1))
  1014. free_swap_slot(entry);
  1015. }
  1016. }
  1017. /*
  1018. * Called after dropping swapcache to decrease refcnt to swap entries.
  1019. */
  1020. static void swapcache_free(swp_entry_t entry)
  1021. {
  1022. struct swap_info_struct *p;
  1023. p = _swap_info_get(entry);
  1024. if (p) {
  1025. if (!__swap_entry_free(p, entry, SWAP_HAS_CACHE))
  1026. free_swap_slot(entry);
  1027. }
  1028. }
  1029. #ifdef CONFIG_THP_SWAP
  1030. static void swapcache_free_cluster(swp_entry_t entry)
  1031. {
  1032. unsigned long offset = swp_offset(entry);
  1033. unsigned long idx = offset / SWAPFILE_CLUSTER;
  1034. struct swap_cluster_info *ci;
  1035. struct swap_info_struct *si;
  1036. unsigned char *map;
  1037. unsigned int i, free_entries = 0;
  1038. unsigned char val;
  1039. si = _swap_info_get(entry);
  1040. if (!si)
  1041. return;
  1042. ci = lock_cluster(si, offset);
  1043. VM_BUG_ON(!cluster_is_huge(ci));
  1044. map = si->swap_map + offset;
  1045. for (i = 0; i < SWAPFILE_CLUSTER; i++) {
  1046. val = map[i];
  1047. VM_BUG_ON(!(val & SWAP_HAS_CACHE));
  1048. if (val == SWAP_HAS_CACHE)
  1049. free_entries++;
  1050. }
  1051. if (!free_entries) {
  1052. for (i = 0; i < SWAPFILE_CLUSTER; i++)
  1053. map[i] &= ~SWAP_HAS_CACHE;
  1054. }
  1055. cluster_clear_huge(ci);
  1056. unlock_cluster(ci);
  1057. if (free_entries == SWAPFILE_CLUSTER) {
  1058. spin_lock(&si->lock);
  1059. ci = lock_cluster(si, offset);
  1060. memset(map, 0, SWAPFILE_CLUSTER);
  1061. unlock_cluster(ci);
  1062. mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER);
  1063. swap_free_cluster(si, idx);
  1064. spin_unlock(&si->lock);
  1065. } else if (free_entries) {
  1066. for (i = 0; i < SWAPFILE_CLUSTER; i++, entry.val++) {
  1067. if (!__swap_entry_free(si, entry, SWAP_HAS_CACHE))
  1068. free_swap_slot(entry);
  1069. }
  1070. }
  1071. }
  1072. int split_swap_cluster(swp_entry_t entry)
  1073. {
  1074. struct swap_info_struct *si;
  1075. struct swap_cluster_info *ci;
  1076. unsigned long offset = swp_offset(entry);
  1077. si = _swap_info_get(entry);
  1078. if (!si)
  1079. return -EBUSY;
  1080. ci = lock_cluster(si, offset);
  1081. cluster_clear_huge(ci);
  1082. unlock_cluster(ci);
  1083. return 0;
  1084. }
  1085. #else
  1086. static inline void swapcache_free_cluster(swp_entry_t entry)
  1087. {
  1088. }
  1089. #endif /* CONFIG_THP_SWAP */
  1090. void put_swap_page(struct page *page, swp_entry_t entry)
  1091. {
  1092. if (!PageTransHuge(page))
  1093. swapcache_free(entry);
  1094. else
  1095. swapcache_free_cluster(entry);
  1096. }
  1097. static int swp_entry_cmp(const void *ent1, const void *ent2)
  1098. {
  1099. const swp_entry_t *e1 = ent1, *e2 = ent2;
  1100. return (int)swp_type(*e1) - (int)swp_type(*e2);
  1101. }
  1102. void swapcache_free_entries(swp_entry_t *entries, int n)
  1103. {
  1104. struct swap_info_struct *p, *prev;
  1105. int i;
  1106. if (n <= 0)
  1107. return;
  1108. prev = NULL;
  1109. p = NULL;
  1110. /*
  1111. * Sort swap entries by swap device, so each lock is only taken once.
  1112. * nr_swapfiles isn't absolutely correct, but the overhead of sort() is
  1113. * so low that it isn't necessary to optimize further.
  1114. */
  1115. if (nr_swapfiles > 1)
  1116. sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);
  1117. for (i = 0; i < n; ++i) {
  1118. p = swap_info_get_cont(entries[i], prev);
  1119. if (p)
  1120. swap_entry_free(p, entries[i]);
  1121. prev = p;
  1122. }
  1123. if (p)
  1124. spin_unlock(&p->lock);
  1125. }
  1126. /*
  1127. * How many references to page are currently swapped out?
  1128. * This does not give an exact answer when swap count is continued,
  1129. * but does include the high COUNT_CONTINUED flag to allow for that.
  1130. */
  1131. int page_swapcount(struct page *page)
  1132. {
  1133. int count = 0;
  1134. struct swap_info_struct *p;
  1135. struct swap_cluster_info *ci;
  1136. swp_entry_t entry;
  1137. unsigned long offset;
  1138. entry.val = page_private(page);
  1139. p = _swap_info_get(entry);
  1140. if (p) {
  1141. offset = swp_offset(entry);
  1142. ci = lock_cluster_or_swap_info(p, offset);
  1143. count = swap_count(p->swap_map[offset]);
  1144. unlock_cluster_or_swap_info(p, ci);
  1145. }
  1146. return count;
  1147. }
  1148. int __swap_count(struct swap_info_struct *si, swp_entry_t entry)
  1149. {
  1150. pgoff_t offset = swp_offset(entry);
  1151. return swap_count(si->swap_map[offset]);
  1152. }
  1153. static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry)
  1154. {
  1155. int count = 0;
  1156. pgoff_t offset = swp_offset(entry);
  1157. struct swap_cluster_info *ci;
  1158. ci = lock_cluster_or_swap_info(si, offset);
  1159. count = swap_count(si->swap_map[offset]);
  1160. unlock_cluster_or_swap_info(si, ci);
  1161. return count;
  1162. }
  1163. /*
  1164. * How many references to @entry are currently swapped out?
  1165. * This does not give an exact answer when swap count is continued,
  1166. * but does include the high COUNT_CONTINUED flag to allow for that.
  1167. */
  1168. int __swp_swapcount(swp_entry_t entry)
  1169. {
  1170. int count = 0;
  1171. struct swap_info_struct *si;
  1172. si = __swap_info_get(entry);
  1173. if (si)
  1174. count = swap_swapcount(si, entry);
  1175. return count;
  1176. }
  1177. /*
  1178. * How many references to @entry are currently swapped out?
  1179. * This considers COUNT_CONTINUED so it returns exact answer.
  1180. */
  1181. int swp_swapcount(swp_entry_t entry)
  1182. {
  1183. int count, tmp_count, n;
  1184. struct swap_info_struct *p;
  1185. struct swap_cluster_info *ci;
  1186. struct page *page;
  1187. pgoff_t offset;
  1188. unsigned char *map;
  1189. p = _swap_info_get(entry);
  1190. if (!p)
  1191. return 0;
  1192. offset = swp_offset(entry);
  1193. ci = lock_cluster_or_swap_info(p, offset);
  1194. count = swap_count(p->swap_map[offset]);
  1195. if (!(count & COUNT_CONTINUED))
  1196. goto out;
  1197. count &= ~COUNT_CONTINUED;
  1198. n = SWAP_MAP_MAX + 1;
  1199. page = vmalloc_to_page(p->swap_map + offset);
  1200. offset &= ~PAGE_MASK;
  1201. VM_BUG_ON(page_private(page) != SWP_CONTINUED);
  1202. do {
  1203. page = list_next_entry(page, lru);
  1204. map = kmap_atomic(page);
  1205. tmp_count = map[offset];
  1206. kunmap_atomic(map);
  1207. count += (tmp_count & ~COUNT_CONTINUED) * n;
  1208. n *= (SWAP_CONT_MAX + 1);
  1209. } while (tmp_count & COUNT_CONTINUED);
  1210. out:
  1211. unlock_cluster_or_swap_info(p, ci);
  1212. return count;
  1213. }
  1214. #ifdef CONFIG_THP_SWAP
  1215. static bool swap_page_trans_huge_swapped(struct swap_info_struct *si,
  1216. swp_entry_t entry)
  1217. {
  1218. struct swap_cluster_info *ci;
  1219. unsigned char *map = si->swap_map;
  1220. unsigned long roffset = swp_offset(entry);
  1221. unsigned long offset = round_down(roffset, SWAPFILE_CLUSTER);
  1222. int i;
  1223. bool ret = false;
  1224. ci = lock_cluster_or_swap_info(si, offset);
  1225. if (!ci || !cluster_is_huge(ci)) {
  1226. if (map[roffset] != SWAP_HAS_CACHE)
  1227. ret = true;
  1228. goto unlock_out;
  1229. }
  1230. for (i = 0; i < SWAPFILE_CLUSTER; i++) {
  1231. if (map[offset + i] != SWAP_HAS_CACHE) {
  1232. ret = true;
  1233. break;
  1234. }
  1235. }
  1236. unlock_out:
  1237. unlock_cluster_or_swap_info(si, ci);
  1238. return ret;
  1239. }
  1240. static bool page_swapped(struct page *page)
  1241. {
  1242. swp_entry_t entry;
  1243. struct swap_info_struct *si;
  1244. if (likely(!PageTransCompound(page)))
  1245. return page_swapcount(page) != 0;
  1246. page = compound_head(page);
  1247. entry.val = page_private(page);
  1248. si = _swap_info_get(entry);
  1249. if (si)
  1250. return swap_page_trans_huge_swapped(si, entry);
  1251. return false;
  1252. }
  1253. static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount,
  1254. int *total_swapcount)
  1255. {
  1256. int i, map_swapcount, _total_mapcount, _total_swapcount;
  1257. unsigned long offset = 0;
  1258. struct swap_info_struct *si;
  1259. struct swap_cluster_info *ci = NULL;
  1260. unsigned char *map = NULL;
  1261. int mapcount, swapcount = 0;
  1262. /* hugetlbfs shouldn't call it */
  1263. VM_BUG_ON_PAGE(PageHuge(page), page);
  1264. if (likely(!PageTransCompound(page))) {
  1265. mapcount = atomic_read(&page->_mapcount) + 1;
  1266. if (total_mapcount)
  1267. *total_mapcount = mapcount;
  1268. if (PageSwapCache(page))
  1269. swapcount = page_swapcount(page);
  1270. if (total_swapcount)
  1271. *total_swapcount = swapcount;
  1272. return mapcount + swapcount;
  1273. }
  1274. page = compound_head(page);
  1275. _total_mapcount = _total_swapcount = map_swapcount = 0;
  1276. if (PageSwapCache(page)) {
  1277. swp_entry_t entry;
  1278. entry.val = page_private(page);
  1279. si = _swap_info_get(entry);
  1280. if (si) {
  1281. map = si->swap_map;
  1282. offset = swp_offset(entry);
  1283. }
  1284. }
  1285. if (map)
  1286. ci = lock_cluster(si, offset);
  1287. for (i = 0; i < HPAGE_PMD_NR; i++) {
  1288. mapcount = atomic_read(&page[i]._mapcount) + 1;
  1289. _total_mapcount += mapcount;
  1290. if (map) {
  1291. swapcount = swap_count(map[offset + i]);
  1292. _total_swapcount += swapcount;
  1293. }
  1294. map_swapcount = max(map_swapcount, mapcount + swapcount);
  1295. }
  1296. unlock_cluster(ci);
  1297. if (PageDoubleMap(page)) {
  1298. map_swapcount -= 1;
  1299. _total_mapcount -= HPAGE_PMD_NR;
  1300. }
  1301. mapcount = compound_mapcount(page);
  1302. map_swapcount += mapcount;
  1303. _total_mapcount += mapcount;
  1304. if (total_mapcount)
  1305. *total_mapcount = _total_mapcount;
  1306. if (total_swapcount)
  1307. *total_swapcount = _total_swapcount;
  1308. return map_swapcount;
  1309. }
  1310. #else
  1311. #define swap_page_trans_huge_swapped(si, entry) swap_swapcount(si, entry)
  1312. #define page_swapped(page) (page_swapcount(page) != 0)
  1313. static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount,
  1314. int *total_swapcount)
  1315. {
  1316. int mapcount, swapcount = 0;
  1317. /* hugetlbfs shouldn't call it */
  1318. VM_BUG_ON_PAGE(PageHuge(page), page);
  1319. mapcount = page_trans_huge_mapcount(page, total_mapcount);
  1320. if (PageSwapCache(page))
  1321. swapcount = page_swapcount(page);
  1322. if (total_swapcount)
  1323. *total_swapcount = swapcount;
  1324. return mapcount + swapcount;
  1325. }
  1326. #endif
  1327. /*
  1328. * We can write to an anon page without COW if there are no other references
  1329. * to it. And as a side-effect, free up its swap: because the old content
  1330. * on disk will never be read, and seeking back there to write new content
  1331. * later would only waste time away from clustering.
  1332. *
  1333. * NOTE: total_map_swapcount should not be relied upon by the caller if
  1334. * reuse_swap_page() returns false, but it may be always overwritten
  1335. * (see the other implementation for CONFIG_SWAP=n).
  1336. */
  1337. bool reuse_swap_page(struct page *page, int *total_map_swapcount)
  1338. {
  1339. int count, total_mapcount, total_swapcount;
  1340. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1341. if (unlikely(PageKsm(page)))
  1342. return false;
  1343. count = page_trans_huge_map_swapcount(page, &total_mapcount,
  1344. &total_swapcount);
  1345. if (total_map_swapcount)
  1346. *total_map_swapcount = total_mapcount + total_swapcount;
  1347. if (count == 1 && PageSwapCache(page) &&
  1348. (likely(!PageTransCompound(page)) ||
  1349. /* The remaining swap count will be freed soon */
  1350. total_swapcount == page_swapcount(page))) {
  1351. if (!PageWriteback(page)) {
  1352. page = compound_head(page);
  1353. delete_from_swap_cache(page);
  1354. SetPageDirty(page);
  1355. } else {
  1356. swp_entry_t entry;
  1357. struct swap_info_struct *p;
  1358. entry.val = page_private(page);
  1359. p = swap_info_get(entry);
  1360. if (p->flags & SWP_STABLE_WRITES) {
  1361. spin_unlock(&p->lock);
  1362. return false;
  1363. }
  1364. spin_unlock(&p->lock);
  1365. }
  1366. }
  1367. return count <= 1;
  1368. }
  1369. /*
  1370. * If swap is getting full, or if there are no more mappings of this page,
  1371. * then try_to_free_swap is called to free its swap space.
  1372. */
  1373. int try_to_free_swap(struct page *page)
  1374. {
  1375. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1376. if (!PageSwapCache(page))
  1377. return 0;
  1378. if (PageWriteback(page))
  1379. return 0;
  1380. if (page_swapped(page))
  1381. return 0;
  1382. /*
  1383. * Once hibernation has begun to create its image of memory,
  1384. * there's a danger that one of the calls to try_to_free_swap()
  1385. * - most probably a call from __try_to_reclaim_swap() while
  1386. * hibernation is allocating its own swap pages for the image,
  1387. * but conceivably even a call from memory reclaim - will free
  1388. * the swap from a page which has already been recorded in the
  1389. * image as a clean swapcache page, and then reuse its swap for
  1390. * another page of the image. On waking from hibernation, the
  1391. * original page might be freed under memory pressure, then
  1392. * later read back in from swap, now with the wrong data.
  1393. *
  1394. * Hibernation suspends storage while it is writing the image
  1395. * to disk so check that here.
  1396. */
  1397. if (pm_suspended_storage())
  1398. return 0;
  1399. page = compound_head(page);
  1400. delete_from_swap_cache(page);
  1401. SetPageDirty(page);
  1402. return 1;
  1403. }
  1404. /*
  1405. * Free the swap entry like above, but also try to
  1406. * free the page cache entry if it is the last user.
  1407. */
  1408. int free_swap_and_cache(swp_entry_t entry)
  1409. {
  1410. struct swap_info_struct *p;
  1411. struct page *page = NULL;
  1412. unsigned char count;
  1413. if (non_swap_entry(entry))
  1414. return 1;
  1415. p = _swap_info_get(entry);
  1416. if (p) {
  1417. count = __swap_entry_free(p, entry, 1);
  1418. if (count == SWAP_HAS_CACHE &&
  1419. !swap_page_trans_huge_swapped(p, entry)) {
  1420. page = find_get_page(swap_address_space(entry),
  1421. swp_offset(entry));
  1422. if (page && !trylock_page(page)) {
  1423. put_page(page);
  1424. page = NULL;
  1425. }
  1426. } else if (!count)
  1427. free_swap_slot(entry);
  1428. }
  1429. if (page) {
  1430. /*
  1431. * Not mapped elsewhere, or swap space full? Free it!
  1432. * Also recheck PageSwapCache now page is locked (above).
  1433. */
  1434. if (PageSwapCache(page) && !PageWriteback(page) &&
  1435. (!page_mapped(page) || mem_cgroup_swap_full(page)) &&
  1436. !swap_page_trans_huge_swapped(p, entry)) {
  1437. page = compound_head(page);
  1438. delete_from_swap_cache(page);
  1439. SetPageDirty(page);
  1440. }
  1441. unlock_page(page);
  1442. put_page(page);
  1443. }
  1444. return p != NULL;
  1445. }
  1446. #ifdef CONFIG_HIBERNATION
  1447. /*
  1448. * Find the swap type that corresponds to given device (if any).
  1449. *
  1450. * @offset - number of the PAGE_SIZE-sized block of the device, starting
  1451. * from 0, in which the swap header is expected to be located.
  1452. *
  1453. * This is needed for the suspend to disk (aka swsusp).
  1454. */
  1455. int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
  1456. {
  1457. struct block_device *bdev = NULL;
  1458. int type;
  1459. if (device)
  1460. bdev = bdget(device);
  1461. spin_lock(&swap_lock);
  1462. for (type = 0; type < nr_swapfiles; type++) {
  1463. struct swap_info_struct *sis = swap_info[type];
  1464. if (!(sis->flags & SWP_WRITEOK))
  1465. continue;
  1466. if (!bdev) {
  1467. if (bdev_p)
  1468. *bdev_p = bdgrab(sis->bdev);
  1469. spin_unlock(&swap_lock);
  1470. return type;
  1471. }
  1472. if (bdev == sis->bdev) {
  1473. struct swap_extent *se = &sis->first_swap_extent;
  1474. if (se->start_block == offset) {
  1475. if (bdev_p)
  1476. *bdev_p = bdgrab(sis->bdev);
  1477. spin_unlock(&swap_lock);
  1478. bdput(bdev);
  1479. return type;
  1480. }
  1481. }
  1482. }
  1483. spin_unlock(&swap_lock);
  1484. if (bdev)
  1485. bdput(bdev);
  1486. return -ENODEV;
  1487. }
  1488. /*
  1489. * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
  1490. * corresponding to given index in swap_info (swap type).
  1491. */
  1492. sector_t swapdev_block(int type, pgoff_t offset)
  1493. {
  1494. struct block_device *bdev;
  1495. if ((unsigned int)type >= nr_swapfiles)
  1496. return 0;
  1497. if (!(swap_info[type]->flags & SWP_WRITEOK))
  1498. return 0;
  1499. return map_swap_entry(swp_entry(type, offset), &bdev);
  1500. }
  1501. /*
  1502. * Return either the total number of swap pages of given type, or the number
  1503. * of free pages of that type (depending on @free)
  1504. *
  1505. * This is needed for software suspend
  1506. */
  1507. unsigned int count_swap_pages(int type, int free)
  1508. {
  1509. unsigned int n = 0;
  1510. spin_lock(&swap_lock);
  1511. if ((unsigned int)type < nr_swapfiles) {
  1512. struct swap_info_struct *sis = swap_info[type];
  1513. spin_lock(&sis->lock);
  1514. if (sis->flags & SWP_WRITEOK) {
  1515. n = sis->pages;
  1516. if (free)
  1517. n -= sis->inuse_pages;
  1518. }
  1519. spin_unlock(&sis->lock);
  1520. }
  1521. spin_unlock(&swap_lock);
  1522. return n;
  1523. }
  1524. #endif /* CONFIG_HIBERNATION */
  1525. static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte)
  1526. {
  1527. return pte_same(pte_swp_clear_soft_dirty(pte), swp_pte);
  1528. }
  1529. /*
  1530. * No need to decide whether this PTE shares the swap entry with others,
  1531. * just let do_wp_page work it out if a write is requested later - to
  1532. * force COW, vm_page_prot omits write permission from any private vma.
  1533. */
  1534. static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
  1535. unsigned long addr, swp_entry_t entry, struct page *page)
  1536. {
  1537. struct page *swapcache;
  1538. struct mem_cgroup *memcg;
  1539. spinlock_t *ptl;
  1540. pte_t *pte;
  1541. int ret = 1;
  1542. swapcache = page;
  1543. page = ksm_might_need_to_copy(page, vma, addr);
  1544. if (unlikely(!page))
  1545. return -ENOMEM;
  1546. if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL,
  1547. &memcg, false)) {
  1548. ret = -ENOMEM;
  1549. goto out_nolock;
  1550. }
  1551. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  1552. if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) {
  1553. mem_cgroup_cancel_charge(page, memcg, false);
  1554. ret = 0;
  1555. goto out;
  1556. }
  1557. dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
  1558. inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
  1559. get_page(page);
  1560. set_pte_at(vma->vm_mm, addr, pte,
  1561. pte_mkold(mk_pte(page, vma->vm_page_prot)));
  1562. if (page == swapcache) {
  1563. page_add_anon_rmap(page, vma, addr, false);
  1564. mem_cgroup_commit_charge(page, memcg, true, false);
  1565. } else { /* ksm created a completely new copy */
  1566. page_add_new_anon_rmap(page, vma, addr, false);
  1567. mem_cgroup_commit_charge(page, memcg, false, false);
  1568. lru_cache_add_active_or_unevictable(page, vma);
  1569. }
  1570. swap_free(entry);
  1571. /*
  1572. * Move the page to the active list so it is not
  1573. * immediately swapped out again after swapon.
  1574. */
  1575. activate_page(page);
  1576. out:
  1577. pte_unmap_unlock(pte, ptl);
  1578. out_nolock:
  1579. if (page != swapcache) {
  1580. unlock_page(page);
  1581. put_page(page);
  1582. }
  1583. return ret;
  1584. }
  1585. static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  1586. unsigned long addr, unsigned long end,
  1587. swp_entry_t entry, struct page *page)
  1588. {
  1589. pte_t swp_pte = swp_entry_to_pte(entry);
  1590. pte_t *pte;
  1591. int ret = 0;
  1592. /*
  1593. * We don't actually need pte lock while scanning for swp_pte: since
  1594. * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
  1595. * page table while we're scanning; though it could get zapped, and on
  1596. * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
  1597. * of unmatched parts which look like swp_pte, so unuse_pte must
  1598. * recheck under pte lock. Scanning without pte lock lets it be
  1599. * preemptable whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
  1600. */
  1601. pte = pte_offset_map(pmd, addr);
  1602. do {
  1603. /*
  1604. * swapoff spends a _lot_ of time in this loop!
  1605. * Test inline before going to call unuse_pte.
  1606. */
  1607. if (unlikely(pte_same_as_swp(*pte, swp_pte))) {
  1608. pte_unmap(pte);
  1609. ret = unuse_pte(vma, pmd, addr, entry, page);
  1610. if (ret)
  1611. goto out;
  1612. pte = pte_offset_map(pmd, addr);
  1613. }
  1614. } while (pte++, addr += PAGE_SIZE, addr != end);
  1615. pte_unmap(pte - 1);
  1616. out:
  1617. return ret;
  1618. }
  1619. static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  1620. unsigned long addr, unsigned long end,
  1621. swp_entry_t entry, struct page *page)
  1622. {
  1623. pmd_t *pmd;
  1624. unsigned long next;
  1625. int ret;
  1626. pmd = pmd_offset(pud, addr);
  1627. do {
  1628. cond_resched();
  1629. next = pmd_addr_end(addr, end);
  1630. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  1631. continue;
  1632. ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
  1633. if (ret)
  1634. return ret;
  1635. } while (pmd++, addr = next, addr != end);
  1636. return 0;
  1637. }
  1638. static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d,
  1639. unsigned long addr, unsigned long end,
  1640. swp_entry_t entry, struct page *page)
  1641. {
  1642. pud_t *pud;
  1643. unsigned long next;
  1644. int ret;
  1645. pud = pud_offset(p4d, addr);
  1646. do {
  1647. next = pud_addr_end(addr, end);
  1648. if (pud_none_or_clear_bad(pud))
  1649. continue;
  1650. ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
  1651. if (ret)
  1652. return ret;
  1653. } while (pud++, addr = next, addr != end);
  1654. return 0;
  1655. }
  1656. static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd,
  1657. unsigned long addr, unsigned long end,
  1658. swp_entry_t entry, struct page *page)
  1659. {
  1660. p4d_t *p4d;
  1661. unsigned long next;
  1662. int ret;
  1663. p4d = p4d_offset(pgd, addr);
  1664. do {
  1665. next = p4d_addr_end(addr, end);
  1666. if (p4d_none_or_clear_bad(p4d))
  1667. continue;
  1668. ret = unuse_pud_range(vma, p4d, addr, next, entry, page);
  1669. if (ret)
  1670. return ret;
  1671. } while (p4d++, addr = next, addr != end);
  1672. return 0;
  1673. }
  1674. static int unuse_vma(struct vm_area_struct *vma,
  1675. swp_entry_t entry, struct page *page)
  1676. {
  1677. pgd_t *pgd;
  1678. unsigned long addr, end, next;
  1679. int ret;
  1680. if (page_anon_vma(page)) {
  1681. addr = page_address_in_vma(page, vma);
  1682. if (addr == -EFAULT)
  1683. return 0;
  1684. else
  1685. end = addr + PAGE_SIZE;
  1686. } else {
  1687. addr = vma->vm_start;
  1688. end = vma->vm_end;
  1689. }
  1690. pgd = pgd_offset(vma->vm_mm, addr);
  1691. do {
  1692. next = pgd_addr_end(addr, end);
  1693. if (pgd_none_or_clear_bad(pgd))
  1694. continue;
  1695. ret = unuse_p4d_range(vma, pgd, addr, next, entry, page);
  1696. if (ret)
  1697. return ret;
  1698. } while (pgd++, addr = next, addr != end);
  1699. return 0;
  1700. }
  1701. static int unuse_mm(struct mm_struct *mm,
  1702. swp_entry_t entry, struct page *page)
  1703. {
  1704. struct vm_area_struct *vma;
  1705. int ret = 0;
  1706. if (!down_read_trylock(&mm->mmap_sem)) {
  1707. /*
  1708. * Activate page so shrink_inactive_list is unlikely to unmap
  1709. * its ptes while lock is dropped, so swapoff can make progress.
  1710. */
  1711. activate_page(page);
  1712. unlock_page(page);
  1713. down_read(&mm->mmap_sem);
  1714. lock_page(page);
  1715. }
  1716. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1717. if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
  1718. break;
  1719. cond_resched();
  1720. }
  1721. up_read(&mm->mmap_sem);
  1722. return (ret < 0)? ret: 0;
  1723. }
  1724. /*
  1725. * Scan swap_map (or frontswap_map if frontswap parameter is true)
  1726. * from current position to next entry still in use.
  1727. * Recycle to start on reaching the end, returning 0 when empty.
  1728. */
  1729. static unsigned int find_next_to_unuse(struct swap_info_struct *si,
  1730. unsigned int prev, bool frontswap)
  1731. {
  1732. unsigned int max = si->max;
  1733. unsigned int i = prev;
  1734. unsigned char count;
  1735. /*
  1736. * No need for swap_lock here: we're just looking
  1737. * for whether an entry is in use, not modifying it; false
  1738. * hits are okay, and sys_swapoff() has already prevented new
  1739. * allocations from this area (while holding swap_lock).
  1740. */
  1741. for (;;) {
  1742. if (++i >= max) {
  1743. if (!prev) {
  1744. i = 0;
  1745. break;
  1746. }
  1747. /*
  1748. * No entries in use at top of swap_map,
  1749. * loop back to start and recheck there.
  1750. */
  1751. max = prev + 1;
  1752. prev = 0;
  1753. i = 1;
  1754. }
  1755. count = READ_ONCE(si->swap_map[i]);
  1756. if (count && swap_count(count) != SWAP_MAP_BAD)
  1757. if (!frontswap || frontswap_test(si, i))
  1758. break;
  1759. if ((i % LATENCY_LIMIT) == 0)
  1760. cond_resched();
  1761. }
  1762. return i;
  1763. }
  1764. /*
  1765. * We completely avoid races by reading each swap page in advance,
  1766. * and then search for the process using it. All the necessary
  1767. * page table adjustments can then be made atomically.
  1768. *
  1769. * if the boolean frontswap is true, only unuse pages_to_unuse pages;
  1770. * pages_to_unuse==0 means all pages; ignored if frontswap is false
  1771. */
  1772. int try_to_unuse(unsigned int type, bool frontswap,
  1773. unsigned long pages_to_unuse)
  1774. {
  1775. struct swap_info_struct *si = swap_info[type];
  1776. struct mm_struct *start_mm;
  1777. volatile unsigned char *swap_map; /* swap_map is accessed without
  1778. * locking. Mark it as volatile
  1779. * to prevent compiler doing
  1780. * something odd.
  1781. */
  1782. unsigned char swcount;
  1783. struct page *page;
  1784. swp_entry_t entry;
  1785. unsigned int i = 0;
  1786. int retval = 0;
  1787. /*
  1788. * When searching mms for an entry, a good strategy is to
  1789. * start at the first mm we freed the previous entry from
  1790. * (though actually we don't notice whether we or coincidence
  1791. * freed the entry). Initialize this start_mm with a hold.
  1792. *
  1793. * A simpler strategy would be to start at the last mm we
  1794. * freed the previous entry from; but that would take less
  1795. * advantage of mmlist ordering, which clusters forked mms
  1796. * together, child after parent. If we race with dup_mmap(), we
  1797. * prefer to resolve parent before child, lest we miss entries
  1798. * duplicated after we scanned child: using last mm would invert
  1799. * that.
  1800. */
  1801. start_mm = &init_mm;
  1802. mmget(&init_mm);
  1803. /*
  1804. * Keep on scanning until all entries have gone. Usually,
  1805. * one pass through swap_map is enough, but not necessarily:
  1806. * there are races when an instance of an entry might be missed.
  1807. */
  1808. while ((i = find_next_to_unuse(si, i, frontswap)) != 0) {
  1809. if (signal_pending(current)) {
  1810. retval = -EINTR;
  1811. break;
  1812. }
  1813. /*
  1814. * Get a page for the entry, using the existing swap
  1815. * cache page if there is one. Otherwise, get a clean
  1816. * page and read the swap into it.
  1817. */
  1818. swap_map = &si->swap_map[i];
  1819. entry = swp_entry(type, i);
  1820. page = read_swap_cache_async(entry,
  1821. GFP_HIGHUSER_MOVABLE, NULL, 0, false);
  1822. if (!page) {
  1823. /*
  1824. * Either swap_duplicate() failed because entry
  1825. * has been freed independently, and will not be
  1826. * reused since sys_swapoff() already disabled
  1827. * allocation from here, or alloc_page() failed.
  1828. */
  1829. swcount = *swap_map;
  1830. /*
  1831. * We don't hold lock here, so the swap entry could be
  1832. * SWAP_MAP_BAD (when the cluster is discarding).
  1833. * Instead of fail out, We can just skip the swap
  1834. * entry because swapoff will wait for discarding
  1835. * finish anyway.
  1836. */
  1837. if (!swcount || swcount == SWAP_MAP_BAD)
  1838. continue;
  1839. retval = -ENOMEM;
  1840. break;
  1841. }
  1842. /*
  1843. * Don't hold on to start_mm if it looks like exiting.
  1844. */
  1845. if (atomic_read(&start_mm->mm_users) == 1) {
  1846. mmput(start_mm);
  1847. start_mm = &init_mm;
  1848. mmget(&init_mm);
  1849. }
  1850. /*
  1851. * Wait for and lock page. When do_swap_page races with
  1852. * try_to_unuse, do_swap_page can handle the fault much
  1853. * faster than try_to_unuse can locate the entry. This
  1854. * apparently redundant "wait_on_page_locked" lets try_to_unuse
  1855. * defer to do_swap_page in such a case - in some tests,
  1856. * do_swap_page and try_to_unuse repeatedly compete.
  1857. */
  1858. wait_on_page_locked(page);
  1859. wait_on_page_writeback(page);
  1860. lock_page(page);
  1861. wait_on_page_writeback(page);
  1862. /*
  1863. * Remove all references to entry.
  1864. */
  1865. swcount = *swap_map;
  1866. if (swap_count(swcount) == SWAP_MAP_SHMEM) {
  1867. retval = shmem_unuse(entry, page);
  1868. /* page has already been unlocked and released */
  1869. if (retval < 0)
  1870. break;
  1871. continue;
  1872. }
  1873. if (swap_count(swcount) && start_mm != &init_mm)
  1874. retval = unuse_mm(start_mm, entry, page);
  1875. if (swap_count(*swap_map)) {
  1876. int set_start_mm = (*swap_map >= swcount);
  1877. struct list_head *p = &start_mm->mmlist;
  1878. struct mm_struct *new_start_mm = start_mm;
  1879. struct mm_struct *prev_mm = start_mm;
  1880. struct mm_struct *mm;
  1881. mmget(new_start_mm);
  1882. mmget(prev_mm);
  1883. spin_lock(&mmlist_lock);
  1884. while (swap_count(*swap_map) && !retval &&
  1885. (p = p->next) != &start_mm->mmlist) {
  1886. mm = list_entry(p, struct mm_struct, mmlist);
  1887. if (!mmget_not_zero(mm))
  1888. continue;
  1889. spin_unlock(&mmlist_lock);
  1890. mmput(prev_mm);
  1891. prev_mm = mm;
  1892. cond_resched();
  1893. swcount = *swap_map;
  1894. if (!swap_count(swcount)) /* any usage ? */
  1895. ;
  1896. else if (mm == &init_mm)
  1897. set_start_mm = 1;
  1898. else
  1899. retval = unuse_mm(mm, entry, page);
  1900. if (set_start_mm && *swap_map < swcount) {
  1901. mmput(new_start_mm);
  1902. mmget(mm);
  1903. new_start_mm = mm;
  1904. set_start_mm = 0;
  1905. }
  1906. spin_lock(&mmlist_lock);
  1907. }
  1908. spin_unlock(&mmlist_lock);
  1909. mmput(prev_mm);
  1910. mmput(start_mm);
  1911. start_mm = new_start_mm;
  1912. }
  1913. if (retval) {
  1914. unlock_page(page);
  1915. put_page(page);
  1916. break;
  1917. }
  1918. /*
  1919. * If a reference remains (rare), we would like to leave
  1920. * the page in the swap cache; but try_to_unmap could
  1921. * then re-duplicate the entry once we drop page lock,
  1922. * so we might loop indefinitely; also, that page could
  1923. * not be swapped out to other storage meanwhile. So:
  1924. * delete from cache even if there's another reference,
  1925. * after ensuring that the data has been saved to disk -
  1926. * since if the reference remains (rarer), it will be
  1927. * read from disk into another page. Splitting into two
  1928. * pages would be incorrect if swap supported "shared
  1929. * private" pages, but they are handled by tmpfs files.
  1930. *
  1931. * Given how unuse_vma() targets one particular offset
  1932. * in an anon_vma, once the anon_vma has been determined,
  1933. * this splitting happens to be just what is needed to
  1934. * handle where KSM pages have been swapped out: re-reading
  1935. * is unnecessarily slow, but we can fix that later on.
  1936. */
  1937. if (swap_count(*swap_map) &&
  1938. PageDirty(page) && PageSwapCache(page)) {
  1939. struct writeback_control wbc = {
  1940. .sync_mode = WB_SYNC_NONE,
  1941. };
  1942. swap_writepage(compound_head(page), &wbc);
  1943. lock_page(page);
  1944. wait_on_page_writeback(page);
  1945. }
  1946. /*
  1947. * It is conceivable that a racing task removed this page from
  1948. * swap cache just before we acquired the page lock at the top,
  1949. * or while we dropped it in unuse_mm(). The page might even
  1950. * be back in swap cache on another swap area: that we must not
  1951. * delete, since it may not have been written out to swap yet.
  1952. */
  1953. if (PageSwapCache(page) &&
  1954. likely(page_private(page) == entry.val) &&
  1955. !page_swapped(page))
  1956. delete_from_swap_cache(compound_head(page));
  1957. /*
  1958. * So we could skip searching mms once swap count went
  1959. * to 1, we did not mark any present ptes as dirty: must
  1960. * mark page dirty so shrink_page_list will preserve it.
  1961. */
  1962. SetPageDirty(page);
  1963. unlock_page(page);
  1964. put_page(page);
  1965. /*
  1966. * Make sure that we aren't completely killing
  1967. * interactive performance.
  1968. */
  1969. cond_resched();
  1970. if (frontswap && pages_to_unuse > 0) {
  1971. if (!--pages_to_unuse)
  1972. break;
  1973. }
  1974. }
  1975. mmput(start_mm);
  1976. return retval;
  1977. }
  1978. /*
  1979. * After a successful try_to_unuse, if no swap is now in use, we know
  1980. * we can empty the mmlist. swap_lock must be held on entry and exit.
  1981. * Note that mmlist_lock nests inside swap_lock, and an mm must be
  1982. * added to the mmlist just after page_duplicate - before would be racy.
  1983. */
  1984. static void drain_mmlist(void)
  1985. {
  1986. struct list_head *p, *next;
  1987. unsigned int type;
  1988. for (type = 0; type < nr_swapfiles; type++)
  1989. if (swap_info[type]->inuse_pages)
  1990. return;
  1991. spin_lock(&mmlist_lock);
  1992. list_for_each_safe(p, next, &init_mm.mmlist)
  1993. list_del_init(p);
  1994. spin_unlock(&mmlist_lock);
  1995. }
  1996. /*
  1997. * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
  1998. * corresponds to page offset for the specified swap entry.
  1999. * Note that the type of this function is sector_t, but it returns page offset
  2000. * into the bdev, not sector offset.
  2001. */
  2002. static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
  2003. {
  2004. struct swap_info_struct *sis;
  2005. struct swap_extent *start_se;
  2006. struct swap_extent *se;
  2007. pgoff_t offset;
  2008. sis = swap_info[swp_type(entry)];
  2009. *bdev = sis->bdev;
  2010. offset = swp_offset(entry);
  2011. start_se = sis->curr_swap_extent;
  2012. se = start_se;
  2013. for ( ; ; ) {
  2014. if (se->start_page <= offset &&
  2015. offset < (se->start_page + se->nr_pages)) {
  2016. return se->start_block + (offset - se->start_page);
  2017. }
  2018. se = list_next_entry(se, list);
  2019. sis->curr_swap_extent = se;
  2020. BUG_ON(se == start_se); /* It *must* be present */
  2021. }
  2022. }
  2023. /*
  2024. * Returns the page offset into bdev for the specified page's swap entry.
  2025. */
  2026. sector_t map_swap_page(struct page *page, struct block_device **bdev)
  2027. {
  2028. swp_entry_t entry;
  2029. entry.val = page_private(page);
  2030. return map_swap_entry(entry, bdev);
  2031. }
  2032. /*
  2033. * Free all of a swapdev's extent information
  2034. */
  2035. static void destroy_swap_extents(struct swap_info_struct *sis)
  2036. {
  2037. while (!list_empty(&sis->first_swap_extent.list)) {
  2038. struct swap_extent *se;
  2039. se = list_first_entry(&sis->first_swap_extent.list,
  2040. struct swap_extent, list);
  2041. list_del(&se->list);
  2042. kfree(se);
  2043. }
  2044. if (sis->flags & SWP_FILE) {
  2045. struct file *swap_file = sis->swap_file;
  2046. struct address_space *mapping = swap_file->f_mapping;
  2047. sis->flags &= ~SWP_FILE;
  2048. mapping->a_ops->swap_deactivate(swap_file);
  2049. }
  2050. }
  2051. /*
  2052. * Add a block range (and the corresponding page range) into this swapdev's
  2053. * extent list. The extent list is kept sorted in page order.
  2054. *
  2055. * This function rather assumes that it is called in ascending page order.
  2056. */
  2057. int
  2058. add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
  2059. unsigned long nr_pages, sector_t start_block)
  2060. {
  2061. struct swap_extent *se;
  2062. struct swap_extent *new_se;
  2063. struct list_head *lh;
  2064. if (start_page == 0) {
  2065. se = &sis->first_swap_extent;
  2066. sis->curr_swap_extent = se;
  2067. se->start_page = 0;
  2068. se->nr_pages = nr_pages;
  2069. se->start_block = start_block;
  2070. return 1;
  2071. } else {
  2072. lh = sis->first_swap_extent.list.prev; /* Highest extent */
  2073. se = list_entry(lh, struct swap_extent, list);
  2074. BUG_ON(se->start_page + se->nr_pages != start_page);
  2075. if (se->start_block + se->nr_pages == start_block) {
  2076. /* Merge it */
  2077. se->nr_pages += nr_pages;
  2078. return 0;
  2079. }
  2080. }
  2081. /*
  2082. * No merge. Insert a new extent, preserving ordering.
  2083. */
  2084. new_se = kmalloc(sizeof(*se), GFP_KERNEL);
  2085. if (new_se == NULL)
  2086. return -ENOMEM;
  2087. new_se->start_page = start_page;
  2088. new_se->nr_pages = nr_pages;
  2089. new_se->start_block = start_block;
  2090. list_add_tail(&new_se->list, &sis->first_swap_extent.list);
  2091. return 1;
  2092. }
  2093. /*
  2094. * A `swap extent' is a simple thing which maps a contiguous range of pages
  2095. * onto a contiguous range of disk blocks. An ordered list of swap extents
  2096. * is built at swapon time and is then used at swap_writepage/swap_readpage
  2097. * time for locating where on disk a page belongs.
  2098. *
  2099. * If the swapfile is an S_ISBLK block device, a single extent is installed.
  2100. * This is done so that the main operating code can treat S_ISBLK and S_ISREG
  2101. * swap files identically.
  2102. *
  2103. * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
  2104. * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
  2105. * swapfiles are handled *identically* after swapon time.
  2106. *
  2107. * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
  2108. * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
  2109. * some stray blocks are found which do not fall within the PAGE_SIZE alignment
  2110. * requirements, they are simply tossed out - we will never use those blocks
  2111. * for swapping.
  2112. *
  2113. * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
  2114. * prevents root from shooting her foot off by ftruncating an in-use swapfile,
  2115. * which will scribble on the fs.
  2116. *
  2117. * The amount of disk space which a single swap extent represents varies.
  2118. * Typically it is in the 1-4 megabyte range. So we can have hundreds of
  2119. * extents in the list. To avoid much list walking, we cache the previous
  2120. * search location in `curr_swap_extent', and start new searches from there.
  2121. * This is extremely effective. The average number of iterations in
  2122. * map_swap_page() has been measured at about 0.3 per page. - akpm.
  2123. */
  2124. static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
  2125. {
  2126. struct file *swap_file = sis->swap_file;
  2127. struct address_space *mapping = swap_file->f_mapping;
  2128. struct inode *inode = mapping->host;
  2129. int ret;
  2130. if (S_ISBLK(inode->i_mode)) {
  2131. ret = add_swap_extent(sis, 0, sis->max, 0);
  2132. *span = sis->pages;
  2133. return ret;
  2134. }
  2135. if (mapping->a_ops->swap_activate) {
  2136. ret = mapping->a_ops->swap_activate(sis, swap_file, span);
  2137. if (!ret) {
  2138. sis->flags |= SWP_FILE;
  2139. ret = add_swap_extent(sis, 0, sis->max, 0);
  2140. *span = sis->pages;
  2141. }
  2142. return ret;
  2143. }
  2144. return generic_swapfile_activate(sis, swap_file, span);
  2145. }
  2146. static int swap_node(struct swap_info_struct *p)
  2147. {
  2148. struct block_device *bdev;
  2149. if (p->bdev)
  2150. bdev = p->bdev;
  2151. else
  2152. bdev = p->swap_file->f_inode->i_sb->s_bdev;
  2153. return bdev ? bdev->bd_disk->node_id : NUMA_NO_NODE;
  2154. }
  2155. static void _enable_swap_info(struct swap_info_struct *p, int prio,
  2156. unsigned char *swap_map,
  2157. struct swap_cluster_info *cluster_info)
  2158. {
  2159. int i;
  2160. if (prio >= 0)
  2161. p->prio = prio;
  2162. else
  2163. p->prio = --least_priority;
  2164. /*
  2165. * the plist prio is negated because plist ordering is
  2166. * low-to-high, while swap ordering is high-to-low
  2167. */
  2168. p->list.prio = -p->prio;
  2169. for_each_node(i) {
  2170. if (p->prio >= 0)
  2171. p->avail_lists[i].prio = -p->prio;
  2172. else {
  2173. if (swap_node(p) == i)
  2174. p->avail_lists[i].prio = 1;
  2175. else
  2176. p->avail_lists[i].prio = -p->prio;
  2177. }
  2178. }
  2179. p->swap_map = swap_map;
  2180. p->cluster_info = cluster_info;
  2181. p->flags |= SWP_WRITEOK;
  2182. atomic_long_add(p->pages, &nr_swap_pages);
  2183. total_swap_pages += p->pages;
  2184. assert_spin_locked(&swap_lock);
  2185. /*
  2186. * both lists are plists, and thus priority ordered.
  2187. * swap_active_head needs to be priority ordered for swapoff(),
  2188. * which on removal of any swap_info_struct with an auto-assigned
  2189. * (i.e. negative) priority increments the auto-assigned priority
  2190. * of any lower-priority swap_info_structs.
  2191. * swap_avail_head needs to be priority ordered for get_swap_page(),
  2192. * which allocates swap pages from the highest available priority
  2193. * swap_info_struct.
  2194. */
  2195. plist_add(&p->list, &swap_active_head);
  2196. add_to_avail_list(p);
  2197. }
  2198. static void enable_swap_info(struct swap_info_struct *p, int prio,
  2199. unsigned char *swap_map,
  2200. struct swap_cluster_info *cluster_info,
  2201. unsigned long *frontswap_map)
  2202. {
  2203. frontswap_init(p->type, frontswap_map);
  2204. spin_lock(&swap_lock);
  2205. spin_lock(&p->lock);
  2206. _enable_swap_info(p, prio, swap_map, cluster_info);
  2207. spin_unlock(&p->lock);
  2208. spin_unlock(&swap_lock);
  2209. }
  2210. static void reinsert_swap_info(struct swap_info_struct *p)
  2211. {
  2212. spin_lock(&swap_lock);
  2213. spin_lock(&p->lock);
  2214. _enable_swap_info(p, p->prio, p->swap_map, p->cluster_info);
  2215. spin_unlock(&p->lock);
  2216. spin_unlock(&swap_lock);
  2217. }
  2218. bool has_usable_swap(void)
  2219. {
  2220. bool ret = true;
  2221. spin_lock(&swap_lock);
  2222. if (plist_head_empty(&swap_active_head))
  2223. ret = false;
  2224. spin_unlock(&swap_lock);
  2225. return ret;
  2226. }
  2227. SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
  2228. {
  2229. struct swap_info_struct *p = NULL;
  2230. unsigned char *swap_map;
  2231. struct swap_cluster_info *cluster_info;
  2232. unsigned long *frontswap_map;
  2233. struct file *swap_file, *victim;
  2234. struct address_space *mapping;
  2235. struct inode *inode;
  2236. struct filename *pathname;
  2237. int err, found = 0;
  2238. unsigned int old_block_size;
  2239. if (!capable(CAP_SYS_ADMIN))
  2240. return -EPERM;
  2241. BUG_ON(!current->mm);
  2242. pathname = getname(specialfile);
  2243. if (IS_ERR(pathname))
  2244. return PTR_ERR(pathname);
  2245. victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
  2246. err = PTR_ERR(victim);
  2247. if (IS_ERR(victim))
  2248. goto out;
  2249. mapping = victim->f_mapping;
  2250. spin_lock(&swap_lock);
  2251. plist_for_each_entry(p, &swap_active_head, list) {
  2252. if (p->flags & SWP_WRITEOK) {
  2253. if (p->swap_file->f_mapping == mapping) {
  2254. found = 1;
  2255. break;
  2256. }
  2257. }
  2258. }
  2259. if (!found) {
  2260. err = -EINVAL;
  2261. spin_unlock(&swap_lock);
  2262. goto out_dput;
  2263. }
  2264. if (!security_vm_enough_memory_mm(current->mm, p->pages))
  2265. vm_unacct_memory(p->pages);
  2266. else {
  2267. err = -ENOMEM;
  2268. spin_unlock(&swap_lock);
  2269. goto out_dput;
  2270. }
  2271. del_from_avail_list(p);
  2272. spin_lock(&p->lock);
  2273. if (p->prio < 0) {
  2274. struct swap_info_struct *si = p;
  2275. int nid;
  2276. plist_for_each_entry_continue(si, &swap_active_head, list) {
  2277. si->prio++;
  2278. si->list.prio--;
  2279. for_each_node(nid) {
  2280. if (si->avail_lists[nid].prio != 1)
  2281. si->avail_lists[nid].prio--;
  2282. }
  2283. }
  2284. least_priority++;
  2285. }
  2286. plist_del(&p->list, &swap_active_head);
  2287. atomic_long_sub(p->pages, &nr_swap_pages);
  2288. total_swap_pages -= p->pages;
  2289. p->flags &= ~SWP_WRITEOK;
  2290. spin_unlock(&p->lock);
  2291. spin_unlock(&swap_lock);
  2292. disable_swap_slots_cache_lock();
  2293. set_current_oom_origin();
  2294. err = try_to_unuse(p->type, false, 0); /* force unuse all pages */
  2295. clear_current_oom_origin();
  2296. if (err) {
  2297. /* re-insert swap space back into swap_list */
  2298. reinsert_swap_info(p);
  2299. reenable_swap_slots_cache_unlock();
  2300. goto out_dput;
  2301. }
  2302. reenable_swap_slots_cache_unlock();
  2303. flush_work(&p->discard_work);
  2304. destroy_swap_extents(p);
  2305. if (p->flags & SWP_CONTINUED)
  2306. free_swap_count_continuations(p);
  2307. if (!p->bdev || !blk_queue_nonrot(bdev_get_queue(p->bdev)))
  2308. atomic_dec(&nr_rotate_swap);
  2309. mutex_lock(&swapon_mutex);
  2310. spin_lock(&swap_lock);
  2311. spin_lock(&p->lock);
  2312. drain_mmlist();
  2313. /* wait for anyone still in scan_swap_map */
  2314. p->highest_bit = 0; /* cuts scans short */
  2315. while (p->flags >= SWP_SCANNING) {
  2316. spin_unlock(&p->lock);
  2317. spin_unlock(&swap_lock);
  2318. schedule_timeout_uninterruptible(1);
  2319. spin_lock(&swap_lock);
  2320. spin_lock(&p->lock);
  2321. }
  2322. swap_file = p->swap_file;
  2323. old_block_size = p->old_block_size;
  2324. p->swap_file = NULL;
  2325. p->max = 0;
  2326. swap_map = p->swap_map;
  2327. p->swap_map = NULL;
  2328. cluster_info = p->cluster_info;
  2329. p->cluster_info = NULL;
  2330. frontswap_map = frontswap_map_get(p);
  2331. spin_unlock(&p->lock);
  2332. spin_unlock(&swap_lock);
  2333. frontswap_invalidate_area(p->type);
  2334. frontswap_map_set(p, NULL);
  2335. mutex_unlock(&swapon_mutex);
  2336. free_percpu(p->percpu_cluster);
  2337. p->percpu_cluster = NULL;
  2338. vfree(swap_map);
  2339. kvfree(cluster_info);
  2340. kvfree(frontswap_map);
  2341. /* Destroy swap account information */
  2342. swap_cgroup_swapoff(p->type);
  2343. exit_swap_address_space(p->type);
  2344. inode = mapping->host;
  2345. if (S_ISBLK(inode->i_mode)) {
  2346. struct block_device *bdev = I_BDEV(inode);
  2347. set_blocksize(bdev, old_block_size);
  2348. blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  2349. } else {
  2350. inode_lock(inode);
  2351. inode->i_flags &= ~S_SWAPFILE;
  2352. inode_unlock(inode);
  2353. }
  2354. filp_close(swap_file, NULL);
  2355. /*
  2356. * Clear the SWP_USED flag after all resources are freed so that swapon
  2357. * can reuse this swap_info in alloc_swap_info() safely. It is ok to
  2358. * not hold p->lock after we cleared its SWP_WRITEOK.
  2359. */
  2360. spin_lock(&swap_lock);
  2361. p->flags = 0;
  2362. spin_unlock(&swap_lock);
  2363. err = 0;
  2364. atomic_inc(&proc_poll_event);
  2365. wake_up_interruptible(&proc_poll_wait);
  2366. out_dput:
  2367. filp_close(victim, NULL);
  2368. out:
  2369. putname(pathname);
  2370. return err;
  2371. }
  2372. #ifdef CONFIG_PROC_FS
  2373. static __poll_t swaps_poll(struct file *file, poll_table *wait)
  2374. {
  2375. struct seq_file *seq = file->private_data;
  2376. poll_wait(file, &proc_poll_wait, wait);
  2377. if (seq->poll_event != atomic_read(&proc_poll_event)) {
  2378. seq->poll_event = atomic_read(&proc_poll_event);
  2379. return EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
  2380. }
  2381. return EPOLLIN | EPOLLRDNORM;
  2382. }
  2383. /* iterator */
  2384. static void *swap_start(struct seq_file *swap, loff_t *pos)
  2385. {
  2386. struct swap_info_struct *si;
  2387. int type;
  2388. loff_t l = *pos;
  2389. mutex_lock(&swapon_mutex);
  2390. if (!l)
  2391. return SEQ_START_TOKEN;
  2392. for (type = 0; type < nr_swapfiles; type++) {
  2393. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  2394. si = swap_info[type];
  2395. if (!(si->flags & SWP_USED) || !si->swap_map)
  2396. continue;
  2397. if (!--l)
  2398. return si;
  2399. }
  2400. return NULL;
  2401. }
  2402. static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
  2403. {
  2404. struct swap_info_struct *si = v;
  2405. int type;
  2406. if (v == SEQ_START_TOKEN)
  2407. type = 0;
  2408. else
  2409. type = si->type + 1;
  2410. for (; type < nr_swapfiles; type++) {
  2411. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  2412. si = swap_info[type];
  2413. if (!(si->flags & SWP_USED) || !si->swap_map)
  2414. continue;
  2415. ++*pos;
  2416. return si;
  2417. }
  2418. return NULL;
  2419. }
  2420. static void swap_stop(struct seq_file *swap, void *v)
  2421. {
  2422. mutex_unlock(&swapon_mutex);
  2423. }
  2424. static int swap_show(struct seq_file *swap, void *v)
  2425. {
  2426. struct swap_info_struct *si = v;
  2427. struct file *file;
  2428. int len;
  2429. if (si == SEQ_START_TOKEN) {
  2430. seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
  2431. return 0;
  2432. }
  2433. file = si->swap_file;
  2434. len = seq_file_path(swap, file, " \t\n\\");
  2435. seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
  2436. len < 40 ? 40 - len : 1, " ",
  2437. S_ISBLK(file_inode(file)->i_mode) ?
  2438. "partition" : "file\t",
  2439. si->pages << (PAGE_SHIFT - 10),
  2440. si->inuse_pages << (PAGE_SHIFT - 10),
  2441. si->prio);
  2442. return 0;
  2443. }
  2444. static const struct seq_operations swaps_op = {
  2445. .start = swap_start,
  2446. .next = swap_next,
  2447. .stop = swap_stop,
  2448. .show = swap_show
  2449. };
  2450. static int swaps_open(struct inode *inode, struct file *file)
  2451. {
  2452. struct seq_file *seq;
  2453. int ret;
  2454. ret = seq_open(file, &swaps_op);
  2455. if (ret)
  2456. return ret;
  2457. seq = file->private_data;
  2458. seq->poll_event = atomic_read(&proc_poll_event);
  2459. return 0;
  2460. }
  2461. static const struct file_operations proc_swaps_operations = {
  2462. .open = swaps_open,
  2463. .read = seq_read,
  2464. .llseek = seq_lseek,
  2465. .release = seq_release,
  2466. .poll = swaps_poll,
  2467. };
  2468. static int __init procswaps_init(void)
  2469. {
  2470. proc_create("swaps", 0, NULL, &proc_swaps_operations);
  2471. return 0;
  2472. }
  2473. __initcall(procswaps_init);
  2474. #endif /* CONFIG_PROC_FS */
  2475. #ifdef MAX_SWAPFILES_CHECK
  2476. static int __init max_swapfiles_check(void)
  2477. {
  2478. MAX_SWAPFILES_CHECK();
  2479. return 0;
  2480. }
  2481. late_initcall(max_swapfiles_check);
  2482. #endif
  2483. static struct swap_info_struct *alloc_swap_info(void)
  2484. {
  2485. struct swap_info_struct *p;
  2486. unsigned int type;
  2487. int i;
  2488. p = kzalloc(sizeof(*p), GFP_KERNEL);
  2489. if (!p)
  2490. return ERR_PTR(-ENOMEM);
  2491. spin_lock(&swap_lock);
  2492. for (type = 0; type < nr_swapfiles; type++) {
  2493. if (!(swap_info[type]->flags & SWP_USED))
  2494. break;
  2495. }
  2496. if (type >= MAX_SWAPFILES) {
  2497. spin_unlock(&swap_lock);
  2498. kfree(p);
  2499. return ERR_PTR(-EPERM);
  2500. }
  2501. if (type >= nr_swapfiles) {
  2502. p->type = type;
  2503. swap_info[type] = p;
  2504. /*
  2505. * Write swap_info[type] before nr_swapfiles, in case a
  2506. * racing procfs swap_start() or swap_next() is reading them.
  2507. * (We never shrink nr_swapfiles, we never free this entry.)
  2508. */
  2509. smp_wmb();
  2510. nr_swapfiles++;
  2511. } else {
  2512. kfree(p);
  2513. p = swap_info[type];
  2514. /*
  2515. * Do not memset this entry: a racing procfs swap_next()
  2516. * would be relying on p->type to remain valid.
  2517. */
  2518. }
  2519. INIT_LIST_HEAD(&p->first_swap_extent.list);
  2520. plist_node_init(&p->list, 0);
  2521. for_each_node(i)
  2522. plist_node_init(&p->avail_lists[i], 0);
  2523. p->flags = SWP_USED;
  2524. spin_unlock(&swap_lock);
  2525. spin_lock_init(&p->lock);
  2526. spin_lock_init(&p->cont_lock);
  2527. return p;
  2528. }
  2529. static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
  2530. {
  2531. int error;
  2532. if (S_ISBLK(inode->i_mode)) {
  2533. p->bdev = bdgrab(I_BDEV(inode));
  2534. error = blkdev_get(p->bdev,
  2535. FMODE_READ | FMODE_WRITE | FMODE_EXCL, p);
  2536. if (error < 0) {
  2537. p->bdev = NULL;
  2538. return error;
  2539. }
  2540. p->old_block_size = block_size(p->bdev);
  2541. error = set_blocksize(p->bdev, PAGE_SIZE);
  2542. if (error < 0)
  2543. return error;
  2544. p->flags |= SWP_BLKDEV;
  2545. } else if (S_ISREG(inode->i_mode)) {
  2546. p->bdev = inode->i_sb->s_bdev;
  2547. inode_lock(inode);
  2548. if (IS_SWAPFILE(inode))
  2549. return -EBUSY;
  2550. } else
  2551. return -EINVAL;
  2552. return 0;
  2553. }
  2554. static unsigned long read_swap_header(struct swap_info_struct *p,
  2555. union swap_header *swap_header,
  2556. struct inode *inode)
  2557. {
  2558. int i;
  2559. unsigned long maxpages;
  2560. unsigned long swapfilepages;
  2561. unsigned long last_page;
  2562. if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
  2563. pr_err("Unable to find swap-space signature\n");
  2564. return 0;
  2565. }
  2566. /* swap partition endianess hack... */
  2567. if (swab32(swap_header->info.version) == 1) {
  2568. swab32s(&swap_header->info.version);
  2569. swab32s(&swap_header->info.last_page);
  2570. swab32s(&swap_header->info.nr_badpages);
  2571. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  2572. return 0;
  2573. for (i = 0; i < swap_header->info.nr_badpages; i++)
  2574. swab32s(&swap_header->info.badpages[i]);
  2575. }
  2576. /* Check the swap header's sub-version */
  2577. if (swap_header->info.version != 1) {
  2578. pr_warn("Unable to handle swap header version %d\n",
  2579. swap_header->info.version);
  2580. return 0;
  2581. }
  2582. p->lowest_bit = 1;
  2583. p->cluster_next = 1;
  2584. p->cluster_nr = 0;
  2585. /*
  2586. * Find out how many pages are allowed for a single swap
  2587. * device. There are two limiting factors: 1) the number
  2588. * of bits for the swap offset in the swp_entry_t type, and
  2589. * 2) the number of bits in the swap pte as defined by the
  2590. * different architectures. In order to find the
  2591. * largest possible bit mask, a swap entry with swap type 0
  2592. * and swap offset ~0UL is created, encoded to a swap pte,
  2593. * decoded to a swp_entry_t again, and finally the swap
  2594. * offset is extracted. This will mask all the bits from
  2595. * the initial ~0UL mask that can't be encoded in either
  2596. * the swp_entry_t or the architecture definition of a
  2597. * swap pte.
  2598. */
  2599. maxpages = swp_offset(pte_to_swp_entry(
  2600. swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
  2601. last_page = swap_header->info.last_page;
  2602. if (!last_page) {
  2603. pr_warn("Empty swap-file\n");
  2604. return 0;
  2605. }
  2606. if (last_page > maxpages) {
  2607. pr_warn("Truncating oversized swap area, only using %luk out of %luk\n",
  2608. maxpages << (PAGE_SHIFT - 10),
  2609. last_page << (PAGE_SHIFT - 10));
  2610. }
  2611. if (maxpages > last_page) {
  2612. maxpages = last_page + 1;
  2613. /* p->max is an unsigned int: don't overflow it */
  2614. if ((unsigned int)maxpages == 0)
  2615. maxpages = UINT_MAX;
  2616. }
  2617. p->highest_bit = maxpages - 1;
  2618. if (!maxpages)
  2619. return 0;
  2620. swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
  2621. if (swapfilepages && maxpages > swapfilepages) {
  2622. pr_warn("Swap area shorter than signature indicates\n");
  2623. return 0;
  2624. }
  2625. if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
  2626. return 0;
  2627. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  2628. return 0;
  2629. return maxpages;
  2630. }
  2631. #define SWAP_CLUSTER_INFO_COLS \
  2632. DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info))
  2633. #define SWAP_CLUSTER_SPACE_COLS \
  2634. DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER)
  2635. #define SWAP_CLUSTER_COLS \
  2636. max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS)
  2637. static int setup_swap_map_and_extents(struct swap_info_struct *p,
  2638. union swap_header *swap_header,
  2639. unsigned char *swap_map,
  2640. struct swap_cluster_info *cluster_info,
  2641. unsigned long maxpages,
  2642. sector_t *span)
  2643. {
  2644. unsigned int j, k;
  2645. unsigned int nr_good_pages;
  2646. int nr_extents;
  2647. unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
  2648. unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS;
  2649. unsigned long i, idx;
  2650. nr_good_pages = maxpages - 1; /* omit header page */
  2651. cluster_list_init(&p->free_clusters);
  2652. cluster_list_init(&p->discard_clusters);
  2653. for (i = 0; i < swap_header->info.nr_badpages; i++) {
  2654. unsigned int page_nr = swap_header->info.badpages[i];
  2655. if (page_nr == 0 || page_nr > swap_header->info.last_page)
  2656. return -EINVAL;
  2657. if (page_nr < maxpages) {
  2658. swap_map[page_nr] = SWAP_MAP_BAD;
  2659. nr_good_pages--;
  2660. /*
  2661. * Haven't marked the cluster free yet, no list
  2662. * operation involved
  2663. */
  2664. inc_cluster_info_page(p, cluster_info, page_nr);
  2665. }
  2666. }
  2667. /* Haven't marked the cluster free yet, no list operation involved */
  2668. for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++)
  2669. inc_cluster_info_page(p, cluster_info, i);
  2670. if (nr_good_pages) {
  2671. swap_map[0] = SWAP_MAP_BAD;
  2672. /*
  2673. * Not mark the cluster free yet, no list
  2674. * operation involved
  2675. */
  2676. inc_cluster_info_page(p, cluster_info, 0);
  2677. p->max = maxpages;
  2678. p->pages = nr_good_pages;
  2679. nr_extents = setup_swap_extents(p, span);
  2680. if (nr_extents < 0)
  2681. return nr_extents;
  2682. nr_good_pages = p->pages;
  2683. }
  2684. if (!nr_good_pages) {
  2685. pr_warn("Empty swap-file\n");
  2686. return -EINVAL;
  2687. }
  2688. if (!cluster_info)
  2689. return nr_extents;
  2690. /*
  2691. * Reduce false cache line sharing between cluster_info and
  2692. * sharing same address space.
  2693. */
  2694. for (k = 0; k < SWAP_CLUSTER_COLS; k++) {
  2695. j = (k + col) % SWAP_CLUSTER_COLS;
  2696. for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) {
  2697. idx = i * SWAP_CLUSTER_COLS + j;
  2698. if (idx >= nr_clusters)
  2699. continue;
  2700. if (cluster_count(&cluster_info[idx]))
  2701. continue;
  2702. cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE);
  2703. cluster_list_add_tail(&p->free_clusters, cluster_info,
  2704. idx);
  2705. }
  2706. }
  2707. return nr_extents;
  2708. }
  2709. /*
  2710. * Helper to sys_swapon determining if a given swap
  2711. * backing device queue supports DISCARD operations.
  2712. */
  2713. static bool swap_discardable(struct swap_info_struct *si)
  2714. {
  2715. struct request_queue *q = bdev_get_queue(si->bdev);
  2716. if (!q || !blk_queue_discard(q))
  2717. return false;
  2718. return true;
  2719. }
  2720. SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
  2721. {
  2722. struct swap_info_struct *p;
  2723. struct filename *name;
  2724. struct file *swap_file = NULL;
  2725. struct address_space *mapping;
  2726. int prio;
  2727. int error;
  2728. union swap_header *swap_header;
  2729. int nr_extents;
  2730. sector_t span;
  2731. unsigned long maxpages;
  2732. unsigned char *swap_map = NULL;
  2733. struct swap_cluster_info *cluster_info = NULL;
  2734. unsigned long *frontswap_map = NULL;
  2735. struct page *page = NULL;
  2736. struct inode *inode = NULL;
  2737. bool inced_nr_rotate_swap = false;
  2738. if (swap_flags & ~SWAP_FLAGS_VALID)
  2739. return -EINVAL;
  2740. if (!capable(CAP_SYS_ADMIN))
  2741. return -EPERM;
  2742. if (!swap_avail_heads)
  2743. return -ENOMEM;
  2744. p = alloc_swap_info();
  2745. if (IS_ERR(p))
  2746. return PTR_ERR(p);
  2747. INIT_WORK(&p->discard_work, swap_discard_work);
  2748. name = getname(specialfile);
  2749. if (IS_ERR(name)) {
  2750. error = PTR_ERR(name);
  2751. name = NULL;
  2752. goto bad_swap;
  2753. }
  2754. swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0);
  2755. if (IS_ERR(swap_file)) {
  2756. error = PTR_ERR(swap_file);
  2757. swap_file = NULL;
  2758. goto bad_swap;
  2759. }
  2760. p->swap_file = swap_file;
  2761. mapping = swap_file->f_mapping;
  2762. inode = mapping->host;
  2763. /* If S_ISREG(inode->i_mode) will do inode_lock(inode); */
  2764. error = claim_swapfile(p, inode);
  2765. if (unlikely(error))
  2766. goto bad_swap;
  2767. /*
  2768. * Read the swap header.
  2769. */
  2770. if (!mapping->a_ops->readpage) {
  2771. error = -EINVAL;
  2772. goto bad_swap;
  2773. }
  2774. page = read_mapping_page(mapping, 0, swap_file);
  2775. if (IS_ERR(page)) {
  2776. error = PTR_ERR(page);
  2777. goto bad_swap;
  2778. }
  2779. swap_header = kmap(page);
  2780. maxpages = read_swap_header(p, swap_header, inode);
  2781. if (unlikely(!maxpages)) {
  2782. error = -EINVAL;
  2783. goto bad_swap;
  2784. }
  2785. /* OK, set up the swap map and apply the bad block list */
  2786. swap_map = vzalloc(maxpages);
  2787. if (!swap_map) {
  2788. error = -ENOMEM;
  2789. goto bad_swap;
  2790. }
  2791. if (bdi_cap_stable_pages_required(inode_to_bdi(inode)))
  2792. p->flags |= SWP_STABLE_WRITES;
  2793. if (bdi_cap_synchronous_io(inode_to_bdi(inode)))
  2794. p->flags |= SWP_SYNCHRONOUS_IO;
  2795. if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) {
  2796. int cpu;
  2797. unsigned long ci, nr_cluster;
  2798. p->flags |= SWP_SOLIDSTATE;
  2799. /*
  2800. * select a random position to start with to help wear leveling
  2801. * SSD
  2802. */
  2803. p->cluster_next = 1 + (prandom_u32() % p->highest_bit);
  2804. nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
  2805. cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info),
  2806. GFP_KERNEL);
  2807. if (!cluster_info) {
  2808. error = -ENOMEM;
  2809. goto bad_swap;
  2810. }
  2811. for (ci = 0; ci < nr_cluster; ci++)
  2812. spin_lock_init(&((cluster_info + ci)->lock));
  2813. p->percpu_cluster = alloc_percpu(struct percpu_cluster);
  2814. if (!p->percpu_cluster) {
  2815. error = -ENOMEM;
  2816. goto bad_swap;
  2817. }
  2818. for_each_possible_cpu(cpu) {
  2819. struct percpu_cluster *cluster;
  2820. cluster = per_cpu_ptr(p->percpu_cluster, cpu);
  2821. cluster_set_null(&cluster->index);
  2822. }
  2823. } else {
  2824. atomic_inc(&nr_rotate_swap);
  2825. inced_nr_rotate_swap = true;
  2826. }
  2827. error = swap_cgroup_swapon(p->type, maxpages);
  2828. if (error)
  2829. goto bad_swap;
  2830. nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
  2831. cluster_info, maxpages, &span);
  2832. if (unlikely(nr_extents < 0)) {
  2833. error = nr_extents;
  2834. goto bad_swap;
  2835. }
  2836. /* frontswap enabled? set up bit-per-page map for frontswap */
  2837. if (IS_ENABLED(CONFIG_FRONTSWAP))
  2838. frontswap_map = kvcalloc(BITS_TO_LONGS(maxpages),
  2839. sizeof(long),
  2840. GFP_KERNEL);
  2841. if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) {
  2842. /*
  2843. * When discard is enabled for swap with no particular
  2844. * policy flagged, we set all swap discard flags here in
  2845. * order to sustain backward compatibility with older
  2846. * swapon(8) releases.
  2847. */
  2848. p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD |
  2849. SWP_PAGE_DISCARD);
  2850. /*
  2851. * By flagging sys_swapon, a sysadmin can tell us to
  2852. * either do single-time area discards only, or to just
  2853. * perform discards for released swap page-clusters.
  2854. * Now it's time to adjust the p->flags accordingly.
  2855. */
  2856. if (swap_flags & SWAP_FLAG_DISCARD_ONCE)
  2857. p->flags &= ~SWP_PAGE_DISCARD;
  2858. else if (swap_flags & SWAP_FLAG_DISCARD_PAGES)
  2859. p->flags &= ~SWP_AREA_DISCARD;
  2860. /* issue a swapon-time discard if it's still required */
  2861. if (p->flags & SWP_AREA_DISCARD) {
  2862. int err = discard_swap(p);
  2863. if (unlikely(err))
  2864. pr_err("swapon: discard_swap(%p): %d\n",
  2865. p, err);
  2866. }
  2867. }
  2868. error = init_swap_address_space(p->type, maxpages);
  2869. if (error)
  2870. goto bad_swap;
  2871. mutex_lock(&swapon_mutex);
  2872. prio = -1;
  2873. if (swap_flags & SWAP_FLAG_PREFER)
  2874. prio =
  2875. (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
  2876. enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map);
  2877. pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s%s\n",
  2878. p->pages<<(PAGE_SHIFT-10), name->name, p->prio,
  2879. nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
  2880. (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
  2881. (p->flags & SWP_DISCARDABLE) ? "D" : "",
  2882. (p->flags & SWP_AREA_DISCARD) ? "s" : "",
  2883. (p->flags & SWP_PAGE_DISCARD) ? "c" : "",
  2884. (frontswap_map) ? "FS" : "");
  2885. mutex_unlock(&swapon_mutex);
  2886. atomic_inc(&proc_poll_event);
  2887. wake_up_interruptible(&proc_poll_wait);
  2888. if (S_ISREG(inode->i_mode))
  2889. inode->i_flags |= S_SWAPFILE;
  2890. error = 0;
  2891. goto out;
  2892. bad_swap:
  2893. free_percpu(p->percpu_cluster);
  2894. p->percpu_cluster = NULL;
  2895. if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
  2896. set_blocksize(p->bdev, p->old_block_size);
  2897. blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  2898. }
  2899. destroy_swap_extents(p);
  2900. swap_cgroup_swapoff(p->type);
  2901. spin_lock(&swap_lock);
  2902. p->swap_file = NULL;
  2903. p->flags = 0;
  2904. spin_unlock(&swap_lock);
  2905. vfree(swap_map);
  2906. kvfree(cluster_info);
  2907. kvfree(frontswap_map);
  2908. if (inced_nr_rotate_swap)
  2909. atomic_dec(&nr_rotate_swap);
  2910. if (swap_file) {
  2911. if (inode && S_ISREG(inode->i_mode)) {
  2912. inode_unlock(inode);
  2913. inode = NULL;
  2914. }
  2915. filp_close(swap_file, NULL);
  2916. }
  2917. out:
  2918. if (page && !IS_ERR(page)) {
  2919. kunmap(page);
  2920. put_page(page);
  2921. }
  2922. if (name)
  2923. putname(name);
  2924. if (inode && S_ISREG(inode->i_mode))
  2925. inode_unlock(inode);
  2926. if (!error)
  2927. enable_swap_slots_cache();
  2928. return error;
  2929. }
  2930. void si_swapinfo(struct sysinfo *val)
  2931. {
  2932. unsigned int type;
  2933. unsigned long nr_to_be_unused = 0;
  2934. spin_lock(&swap_lock);
  2935. for (type = 0; type < nr_swapfiles; type++) {
  2936. struct swap_info_struct *si = swap_info[type];
  2937. if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
  2938. nr_to_be_unused += si->inuse_pages;
  2939. }
  2940. val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused;
  2941. val->totalswap = total_swap_pages + nr_to_be_unused;
  2942. spin_unlock(&swap_lock);
  2943. }
  2944. /*
  2945. * Verify that a swap entry is valid and increment its swap map count.
  2946. *
  2947. * Returns error code in following case.
  2948. * - success -> 0
  2949. * - swp_entry is invalid -> EINVAL
  2950. * - swp_entry is migration entry -> EINVAL
  2951. * - swap-cache reference is requested but there is already one. -> EEXIST
  2952. * - swap-cache reference is requested but the entry is not used. -> ENOENT
  2953. * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
  2954. */
  2955. static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
  2956. {
  2957. struct swap_info_struct *p;
  2958. struct swap_cluster_info *ci;
  2959. unsigned long offset, type;
  2960. unsigned char count;
  2961. unsigned char has_cache;
  2962. int err = -EINVAL;
  2963. if (non_swap_entry(entry))
  2964. goto out;
  2965. type = swp_type(entry);
  2966. if (type >= nr_swapfiles)
  2967. goto bad_file;
  2968. p = swap_info[type];
  2969. offset = swp_offset(entry);
  2970. if (unlikely(offset >= p->max))
  2971. goto out;
  2972. ci = lock_cluster_or_swap_info(p, offset);
  2973. count = p->swap_map[offset];
  2974. /*
  2975. * swapin_readahead() doesn't check if a swap entry is valid, so the
  2976. * swap entry could be SWAP_MAP_BAD. Check here with lock held.
  2977. */
  2978. if (unlikely(swap_count(count) == SWAP_MAP_BAD)) {
  2979. err = -ENOENT;
  2980. goto unlock_out;
  2981. }
  2982. has_cache = count & SWAP_HAS_CACHE;
  2983. count &= ~SWAP_HAS_CACHE;
  2984. err = 0;
  2985. if (usage == SWAP_HAS_CACHE) {
  2986. /* set SWAP_HAS_CACHE if there is no cache and entry is used */
  2987. if (!has_cache && count)
  2988. has_cache = SWAP_HAS_CACHE;
  2989. else if (has_cache) /* someone else added cache */
  2990. err = -EEXIST;
  2991. else /* no users remaining */
  2992. err = -ENOENT;
  2993. } else if (count || has_cache) {
  2994. if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
  2995. count += usage;
  2996. else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
  2997. err = -EINVAL;
  2998. else if (swap_count_continued(p, offset, count))
  2999. count = COUNT_CONTINUED;
  3000. else
  3001. err = -ENOMEM;
  3002. } else
  3003. err = -ENOENT; /* unused swap entry */
  3004. p->swap_map[offset] = count | has_cache;
  3005. unlock_out:
  3006. unlock_cluster_or_swap_info(p, ci);
  3007. out:
  3008. return err;
  3009. bad_file:
  3010. pr_err("swap_dup: %s%08lx\n", Bad_file, entry.val);
  3011. goto out;
  3012. }
  3013. /*
  3014. * Help swapoff by noting that swap entry belongs to shmem/tmpfs
  3015. * (in which case its reference count is never incremented).
  3016. */
  3017. void swap_shmem_alloc(swp_entry_t entry)
  3018. {
  3019. __swap_duplicate(entry, SWAP_MAP_SHMEM);
  3020. }
  3021. /*
  3022. * Increase reference count of swap entry by 1.
  3023. * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
  3024. * but could not be atomically allocated. Returns 0, just as if it succeeded,
  3025. * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
  3026. * might occur if a page table entry has got corrupted.
  3027. */
  3028. int swap_duplicate(swp_entry_t entry)
  3029. {
  3030. int err = 0;
  3031. while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
  3032. err = add_swap_count_continuation(entry, GFP_ATOMIC);
  3033. return err;
  3034. }
  3035. /*
  3036. * @entry: swap entry for which we allocate swap cache.
  3037. *
  3038. * Called when allocating swap cache for existing swap entry,
  3039. * This can return error codes. Returns 0 at success.
  3040. * -EBUSY means there is a swap cache.
  3041. * Note: return code is different from swap_duplicate().
  3042. */
  3043. int swapcache_prepare(swp_entry_t entry)
  3044. {
  3045. return __swap_duplicate(entry, SWAP_HAS_CACHE);
  3046. }
  3047. struct swap_info_struct *swp_swap_info(swp_entry_t entry)
  3048. {
  3049. return swap_info[swp_type(entry)];
  3050. }
  3051. struct swap_info_struct *page_swap_info(struct page *page)
  3052. {
  3053. swp_entry_t entry = { .val = page_private(page) };
  3054. return swp_swap_info(entry);
  3055. }
  3056. /*
  3057. * out-of-line __page_file_ methods to avoid include hell.
  3058. */
  3059. struct address_space *__page_file_mapping(struct page *page)
  3060. {
  3061. return page_swap_info(page)->swap_file->f_mapping;
  3062. }
  3063. EXPORT_SYMBOL_GPL(__page_file_mapping);
  3064. pgoff_t __page_file_index(struct page *page)
  3065. {
  3066. swp_entry_t swap = { .val = page_private(page) };
  3067. return swp_offset(swap);
  3068. }
  3069. EXPORT_SYMBOL_GPL(__page_file_index);
  3070. /*
  3071. * add_swap_count_continuation - called when a swap count is duplicated
  3072. * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
  3073. * page of the original vmalloc'ed swap_map, to hold the continuation count
  3074. * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called
  3075. * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
  3076. *
  3077. * These continuation pages are seldom referenced: the common paths all work
  3078. * on the original swap_map, only referring to a continuation page when the
  3079. * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
  3080. *
  3081. * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
  3082. * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
  3083. * can be called after dropping locks.
  3084. */
  3085. int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
  3086. {
  3087. struct swap_info_struct *si;
  3088. struct swap_cluster_info *ci;
  3089. struct page *head;
  3090. struct page *page;
  3091. struct page *list_page;
  3092. pgoff_t offset;
  3093. unsigned char count;
  3094. /*
  3095. * When debugging, it's easier to use __GFP_ZERO here; but it's better
  3096. * for latency not to zero a page while GFP_ATOMIC and holding locks.
  3097. */
  3098. page = alloc_page(gfp_mask | __GFP_HIGHMEM);
  3099. si = swap_info_get(entry);
  3100. if (!si) {
  3101. /*
  3102. * An acceptable race has occurred since the failing
  3103. * __swap_duplicate(): the swap entry has been freed,
  3104. * perhaps even the whole swap_map cleared for swapoff.
  3105. */
  3106. goto outer;
  3107. }
  3108. offset = swp_offset(entry);
  3109. ci = lock_cluster(si, offset);
  3110. count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
  3111. if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
  3112. /*
  3113. * The higher the swap count, the more likely it is that tasks
  3114. * will race to add swap count continuation: we need to avoid
  3115. * over-provisioning.
  3116. */
  3117. goto out;
  3118. }
  3119. if (!page) {
  3120. unlock_cluster(ci);
  3121. spin_unlock(&si->lock);
  3122. return -ENOMEM;
  3123. }
  3124. /*
  3125. * We are fortunate that although vmalloc_to_page uses pte_offset_map,
  3126. * no architecture is using highmem pages for kernel page tables: so it
  3127. * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps.
  3128. */
  3129. head = vmalloc_to_page(si->swap_map + offset);
  3130. offset &= ~PAGE_MASK;
  3131. spin_lock(&si->cont_lock);
  3132. /*
  3133. * Page allocation does not initialize the page's lru field,
  3134. * but it does always reset its private field.
  3135. */
  3136. if (!page_private(head)) {
  3137. BUG_ON(count & COUNT_CONTINUED);
  3138. INIT_LIST_HEAD(&head->lru);
  3139. set_page_private(head, SWP_CONTINUED);
  3140. si->flags |= SWP_CONTINUED;
  3141. }
  3142. list_for_each_entry(list_page, &head->lru, lru) {
  3143. unsigned char *map;
  3144. /*
  3145. * If the previous map said no continuation, but we've found
  3146. * a continuation page, free our allocation and use this one.
  3147. */
  3148. if (!(count & COUNT_CONTINUED))
  3149. goto out_unlock_cont;
  3150. map = kmap_atomic(list_page) + offset;
  3151. count = *map;
  3152. kunmap_atomic(map);
  3153. /*
  3154. * If this continuation count now has some space in it,
  3155. * free our allocation and use this one.
  3156. */
  3157. if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
  3158. goto out_unlock_cont;
  3159. }
  3160. list_add_tail(&page->lru, &head->lru);
  3161. page = NULL; /* now it's attached, don't free it */
  3162. out_unlock_cont:
  3163. spin_unlock(&si->cont_lock);
  3164. out:
  3165. unlock_cluster(ci);
  3166. spin_unlock(&si->lock);
  3167. outer:
  3168. if (page)
  3169. __free_page(page);
  3170. return 0;
  3171. }
  3172. /*
  3173. * swap_count_continued - when the original swap_map count is incremented
  3174. * from SWAP_MAP_MAX, check if there is already a continuation page to carry
  3175. * into, carry if so, or else fail until a new continuation page is allocated;
  3176. * when the original swap_map count is decremented from 0 with continuation,
  3177. * borrow from the continuation and report whether it still holds more.
  3178. * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster
  3179. * lock.
  3180. */
  3181. static bool swap_count_continued(struct swap_info_struct *si,
  3182. pgoff_t offset, unsigned char count)
  3183. {
  3184. struct page *head;
  3185. struct page *page;
  3186. unsigned char *map;
  3187. bool ret;
  3188. head = vmalloc_to_page(si->swap_map + offset);
  3189. if (page_private(head) != SWP_CONTINUED) {
  3190. BUG_ON(count & COUNT_CONTINUED);
  3191. return false; /* need to add count continuation */
  3192. }
  3193. spin_lock(&si->cont_lock);
  3194. offset &= ~PAGE_MASK;
  3195. page = list_entry(head->lru.next, struct page, lru);
  3196. map = kmap_atomic(page) + offset;
  3197. if (count == SWAP_MAP_MAX) /* initial increment from swap_map */
  3198. goto init_map; /* jump over SWAP_CONT_MAX checks */
  3199. if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
  3200. /*
  3201. * Think of how you add 1 to 999
  3202. */
  3203. while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
  3204. kunmap_atomic(map);
  3205. page = list_entry(page->lru.next, struct page, lru);
  3206. BUG_ON(page == head);
  3207. map = kmap_atomic(page) + offset;
  3208. }
  3209. if (*map == SWAP_CONT_MAX) {
  3210. kunmap_atomic(map);
  3211. page = list_entry(page->lru.next, struct page, lru);
  3212. if (page == head) {
  3213. ret = false; /* add count continuation */
  3214. goto out;
  3215. }
  3216. map = kmap_atomic(page) + offset;
  3217. init_map: *map = 0; /* we didn't zero the page */
  3218. }
  3219. *map += 1;
  3220. kunmap_atomic(map);
  3221. page = list_entry(page->lru.prev, struct page, lru);
  3222. while (page != head) {
  3223. map = kmap_atomic(page) + offset;
  3224. *map = COUNT_CONTINUED;
  3225. kunmap_atomic(map);
  3226. page = list_entry(page->lru.prev, struct page, lru);
  3227. }
  3228. ret = true; /* incremented */
  3229. } else { /* decrementing */
  3230. /*
  3231. * Think of how you subtract 1 from 1000
  3232. */
  3233. BUG_ON(count != COUNT_CONTINUED);
  3234. while (*map == COUNT_CONTINUED) {
  3235. kunmap_atomic(map);
  3236. page = list_entry(page->lru.next, struct page, lru);
  3237. BUG_ON(page == head);
  3238. map = kmap_atomic(page) + offset;
  3239. }
  3240. BUG_ON(*map == 0);
  3241. *map -= 1;
  3242. if (*map == 0)
  3243. count = 0;
  3244. kunmap_atomic(map);
  3245. page = list_entry(page->lru.prev, struct page, lru);
  3246. while (page != head) {
  3247. map = kmap_atomic(page) + offset;
  3248. *map = SWAP_CONT_MAX | count;
  3249. count = COUNT_CONTINUED;
  3250. kunmap_atomic(map);
  3251. page = list_entry(page->lru.prev, struct page, lru);
  3252. }
  3253. ret = count == COUNT_CONTINUED;
  3254. }
  3255. out:
  3256. spin_unlock(&si->cont_lock);
  3257. return ret;
  3258. }
  3259. /*
  3260. * free_swap_count_continuations - swapoff free all the continuation pages
  3261. * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
  3262. */
  3263. static void free_swap_count_continuations(struct swap_info_struct *si)
  3264. {
  3265. pgoff_t offset;
  3266. for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
  3267. struct page *head;
  3268. head = vmalloc_to_page(si->swap_map + offset);
  3269. if (page_private(head)) {
  3270. struct page *page, *next;
  3271. list_for_each_entry_safe(page, next, &head->lru, lru) {
  3272. list_del(&page->lru);
  3273. __free_page(page);
  3274. }
  3275. }
  3276. }
  3277. }
  3278. static int __init swapfile_init(void)
  3279. {
  3280. int nid;
  3281. swap_avail_heads = kmalloc_array(nr_node_ids, sizeof(struct plist_head),
  3282. GFP_KERNEL);
  3283. if (!swap_avail_heads) {
  3284. pr_emerg("Not enough memory for swap heads, swap is disabled\n");
  3285. return -ENOMEM;
  3286. }
  3287. for_each_node(nid)
  3288. plist_head_init(&swap_avail_heads[nid]);
  3289. return 0;
  3290. }
  3291. subsys_initcall(swapfile_init);