swapfile.c 96 KB

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