swapfile.c 81 KB

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