swapfile.c 96 KB

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