swapfile.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559
  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/kernel_stat.h>
  12. #include <linux/swap.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/namei.h>
  16. #include <linux/shm.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/random.h>
  19. #include <linux/writeback.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/init.h>
  23. #include <linux/module.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 <asm/pgtable.h>
  34. #include <asm/tlbflush.h>
  35. #include <linux/swapops.h>
  36. #include <linux/page_cgroup.h>
  37. static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
  38. unsigned char);
  39. static void free_swap_count_continuations(struct swap_info_struct *);
  40. static sector_t map_swap_entry(swp_entry_t, struct block_device**);
  41. static DEFINE_SPINLOCK(swap_lock);
  42. static unsigned int nr_swapfiles;
  43. long nr_swap_pages;
  44. long total_swap_pages;
  45. static int least_priority;
  46. static const char Bad_file[] = "Bad swap file entry ";
  47. static const char Unused_file[] = "Unused swap file entry ";
  48. static const char Bad_offset[] = "Bad swap offset entry ";
  49. static const char Unused_offset[] = "Unused swap offset entry ";
  50. static struct swap_list_t swap_list = {-1, -1};
  51. static struct swap_info_struct *swap_info[MAX_SWAPFILES];
  52. static DEFINE_MUTEX(swapon_mutex);
  53. static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
  54. /* Activity counter to indicate that a swapon or swapoff has occurred */
  55. static atomic_t proc_poll_event = ATOMIC_INIT(0);
  56. static inline unsigned char swap_count(unsigned char ent)
  57. {
  58. return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */
  59. }
  60. /* returns 1 if swap entry is freed */
  61. static int
  62. __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
  63. {
  64. swp_entry_t entry = swp_entry(si->type, offset);
  65. struct page *page;
  66. int ret = 0;
  67. page = find_get_page(&swapper_space, entry.val);
  68. if (!page)
  69. return 0;
  70. /*
  71. * This function is called from scan_swap_map() and it's called
  72. * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
  73. * We have to use trylock for avoiding deadlock. This is a special
  74. * case and you should use try_to_free_swap() with explicit lock_page()
  75. * in usual operations.
  76. */
  77. if (trylock_page(page)) {
  78. ret = try_to_free_swap(page);
  79. unlock_page(page);
  80. }
  81. page_cache_release(page);
  82. return ret;
  83. }
  84. /*
  85. * We need this because the bdev->unplug_fn can sleep and we cannot
  86. * hold swap_lock while calling the unplug_fn. And swap_lock
  87. * cannot be turned into a mutex.
  88. */
  89. static DECLARE_RWSEM(swap_unplug_sem);
  90. void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
  91. {
  92. swp_entry_t entry;
  93. down_read(&swap_unplug_sem);
  94. entry.val = page_private(page);
  95. if (PageSwapCache(page)) {
  96. struct block_device *bdev = swap_info[swp_type(entry)]->bdev;
  97. struct backing_dev_info *bdi;
  98. /*
  99. * If the page is removed from swapcache from under us (with a
  100. * racy try_to_unuse/swapoff) we need an additional reference
  101. * count to avoid reading garbage from page_private(page) above.
  102. * If the WARN_ON triggers during a swapoff it maybe the race
  103. * condition and it's harmless. However if it triggers without
  104. * swapoff it signals a problem.
  105. */
  106. WARN_ON(page_count(page) <= 1);
  107. bdi = bdev->bd_inode->i_mapping->backing_dev_info;
  108. blk_run_backing_dev(bdi, page);
  109. }
  110. up_read(&swap_unplug_sem);
  111. }
  112. /*
  113. * swapon tell device that all the old swap contents can be discarded,
  114. * to allow the swap device to optimize its wear-levelling.
  115. */
  116. static int discard_swap(struct swap_info_struct *si)
  117. {
  118. struct swap_extent *se;
  119. sector_t start_block;
  120. sector_t nr_blocks;
  121. int err = 0;
  122. /* Do not discard the swap header page! */
  123. se = &si->first_swap_extent;
  124. start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
  125. nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
  126. if (nr_blocks) {
  127. err = blkdev_issue_discard(si->bdev, start_block,
  128. nr_blocks, GFP_KERNEL, 0);
  129. if (err)
  130. return err;
  131. cond_resched();
  132. }
  133. list_for_each_entry(se, &si->first_swap_extent.list, list) {
  134. start_block = se->start_block << (PAGE_SHIFT - 9);
  135. nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
  136. err = blkdev_issue_discard(si->bdev, start_block,
  137. nr_blocks, GFP_KERNEL, 0);
  138. if (err)
  139. break;
  140. cond_resched();
  141. }
  142. return err; /* That will often be -EOPNOTSUPP */
  143. }
  144. /*
  145. * swap allocation tell device that a cluster of swap can now be discarded,
  146. * to allow the swap device to optimize its wear-levelling.
  147. */
  148. static void discard_swap_cluster(struct swap_info_struct *si,
  149. pgoff_t start_page, pgoff_t nr_pages)
  150. {
  151. struct swap_extent *se = si->curr_swap_extent;
  152. int found_extent = 0;
  153. while (nr_pages) {
  154. struct list_head *lh;
  155. if (se->start_page <= start_page &&
  156. start_page < se->start_page + se->nr_pages) {
  157. pgoff_t offset = start_page - se->start_page;
  158. sector_t start_block = se->start_block + offset;
  159. sector_t nr_blocks = se->nr_pages - offset;
  160. if (nr_blocks > nr_pages)
  161. nr_blocks = nr_pages;
  162. start_page += nr_blocks;
  163. nr_pages -= nr_blocks;
  164. if (!found_extent++)
  165. si->curr_swap_extent = se;
  166. start_block <<= PAGE_SHIFT - 9;
  167. nr_blocks <<= PAGE_SHIFT - 9;
  168. if (blkdev_issue_discard(si->bdev, start_block,
  169. nr_blocks, GFP_NOIO, 0))
  170. break;
  171. }
  172. lh = se->list.next;
  173. se = list_entry(lh, struct swap_extent, list);
  174. }
  175. }
  176. static int wait_for_discard(void *word)
  177. {
  178. schedule();
  179. return 0;
  180. }
  181. #define SWAPFILE_CLUSTER 256
  182. #define LATENCY_LIMIT 256
  183. static inline unsigned long scan_swap_map(struct swap_info_struct *si,
  184. unsigned char usage)
  185. {
  186. unsigned long offset;
  187. unsigned long scan_base;
  188. unsigned long last_in_cluster = 0;
  189. int latency_ration = LATENCY_LIMIT;
  190. int found_free_cluster = 0;
  191. /*
  192. * We try to cluster swap pages by allocating them sequentially
  193. * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
  194. * way, however, we resort to first-free allocation, starting
  195. * a new cluster. This prevents us from scattering swap pages
  196. * all over the entire swap partition, so that we reduce
  197. * overall disk seek times between swap pages. -- sct
  198. * But we do now try to find an empty cluster. -Andrea
  199. * And we let swap pages go all over an SSD partition. Hugh
  200. */
  201. si->flags += SWP_SCANNING;
  202. scan_base = offset = si->cluster_next;
  203. if (unlikely(!si->cluster_nr--)) {
  204. if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
  205. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  206. goto checks;
  207. }
  208. if (si->flags & SWP_DISCARDABLE) {
  209. /*
  210. * Start range check on racing allocations, in case
  211. * they overlap the cluster we eventually decide on
  212. * (we scan without swap_lock to allow preemption).
  213. * It's hardly conceivable that cluster_nr could be
  214. * wrapped during our scan, but don't depend on it.
  215. */
  216. if (si->lowest_alloc)
  217. goto checks;
  218. si->lowest_alloc = si->max;
  219. si->highest_alloc = 0;
  220. }
  221. spin_unlock(&swap_lock);
  222. /*
  223. * If seek is expensive, start searching for new cluster from
  224. * start of partition, to minimize the span of allocated swap.
  225. * But if seek is cheap, search from our current position, so
  226. * that swap is allocated from all over the partition: if the
  227. * Flash Translation Layer only remaps within limited zones,
  228. * we don't want to wear out the first zone too quickly.
  229. */
  230. if (!(si->flags & SWP_SOLIDSTATE))
  231. scan_base = offset = si->lowest_bit;
  232. last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
  233. /* Locate the first empty (unaligned) cluster */
  234. for (; last_in_cluster <= si->highest_bit; offset++) {
  235. if (si->swap_map[offset])
  236. last_in_cluster = offset + SWAPFILE_CLUSTER;
  237. else if (offset == last_in_cluster) {
  238. spin_lock(&swap_lock);
  239. offset -= SWAPFILE_CLUSTER - 1;
  240. si->cluster_next = offset;
  241. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  242. found_free_cluster = 1;
  243. goto checks;
  244. }
  245. if (unlikely(--latency_ration < 0)) {
  246. cond_resched();
  247. latency_ration = LATENCY_LIMIT;
  248. }
  249. }
  250. offset = si->lowest_bit;
  251. last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
  252. /* Locate the first empty (unaligned) cluster */
  253. for (; last_in_cluster < scan_base; offset++) {
  254. if (si->swap_map[offset])
  255. last_in_cluster = offset + SWAPFILE_CLUSTER;
  256. else if (offset == last_in_cluster) {
  257. spin_lock(&swap_lock);
  258. offset -= SWAPFILE_CLUSTER - 1;
  259. si->cluster_next = offset;
  260. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  261. found_free_cluster = 1;
  262. goto checks;
  263. }
  264. if (unlikely(--latency_ration < 0)) {
  265. cond_resched();
  266. latency_ration = LATENCY_LIMIT;
  267. }
  268. }
  269. offset = scan_base;
  270. spin_lock(&swap_lock);
  271. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  272. si->lowest_alloc = 0;
  273. }
  274. checks:
  275. if (!(si->flags & SWP_WRITEOK))
  276. goto no_page;
  277. if (!si->highest_bit)
  278. goto no_page;
  279. if (offset > si->highest_bit)
  280. scan_base = offset = si->lowest_bit;
  281. /* reuse swap entry of cache-only swap if not busy. */
  282. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  283. int swap_was_freed;
  284. spin_unlock(&swap_lock);
  285. swap_was_freed = __try_to_reclaim_swap(si, offset);
  286. spin_lock(&swap_lock);
  287. /* entry was freed successfully, try to use this again */
  288. if (swap_was_freed)
  289. goto checks;
  290. goto scan; /* check next one */
  291. }
  292. if (si->swap_map[offset])
  293. goto scan;
  294. if (offset == si->lowest_bit)
  295. si->lowest_bit++;
  296. if (offset == si->highest_bit)
  297. si->highest_bit--;
  298. si->inuse_pages++;
  299. if (si->inuse_pages == si->pages) {
  300. si->lowest_bit = si->max;
  301. si->highest_bit = 0;
  302. }
  303. si->swap_map[offset] = usage;
  304. si->cluster_next = offset + 1;
  305. si->flags -= SWP_SCANNING;
  306. if (si->lowest_alloc) {
  307. /*
  308. * Only set when SWP_DISCARDABLE, and there's a scan
  309. * for a free cluster in progress or just completed.
  310. */
  311. if (found_free_cluster) {
  312. /*
  313. * To optimize wear-levelling, discard the
  314. * old data of the cluster, taking care not to
  315. * discard any of its pages that have already
  316. * been allocated by racing tasks (offset has
  317. * already stepped over any at the beginning).
  318. */
  319. if (offset < si->highest_alloc &&
  320. si->lowest_alloc <= last_in_cluster)
  321. last_in_cluster = si->lowest_alloc - 1;
  322. si->flags |= SWP_DISCARDING;
  323. spin_unlock(&swap_lock);
  324. if (offset < last_in_cluster)
  325. discard_swap_cluster(si, offset,
  326. last_in_cluster - offset + 1);
  327. spin_lock(&swap_lock);
  328. si->lowest_alloc = 0;
  329. si->flags &= ~SWP_DISCARDING;
  330. smp_mb(); /* wake_up_bit advises this */
  331. wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
  332. } else if (si->flags & SWP_DISCARDING) {
  333. /*
  334. * Delay using pages allocated by racing tasks
  335. * until the whole discard has been issued. We
  336. * could defer that delay until swap_writepage,
  337. * but it's easier to keep this self-contained.
  338. */
  339. spin_unlock(&swap_lock);
  340. wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
  341. wait_for_discard, TASK_UNINTERRUPTIBLE);
  342. spin_lock(&swap_lock);
  343. } else {
  344. /*
  345. * Note pages allocated by racing tasks while
  346. * scan for a free cluster is in progress, so
  347. * that its final discard can exclude them.
  348. */
  349. if (offset < si->lowest_alloc)
  350. si->lowest_alloc = offset;
  351. if (offset > si->highest_alloc)
  352. si->highest_alloc = offset;
  353. }
  354. }
  355. return offset;
  356. scan:
  357. spin_unlock(&swap_lock);
  358. while (++offset <= si->highest_bit) {
  359. if (!si->swap_map[offset]) {
  360. spin_lock(&swap_lock);
  361. goto checks;
  362. }
  363. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  364. spin_lock(&swap_lock);
  365. goto checks;
  366. }
  367. if (unlikely(--latency_ration < 0)) {
  368. cond_resched();
  369. latency_ration = LATENCY_LIMIT;
  370. }
  371. }
  372. offset = si->lowest_bit;
  373. while (++offset < scan_base) {
  374. if (!si->swap_map[offset]) {
  375. spin_lock(&swap_lock);
  376. goto checks;
  377. }
  378. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  379. spin_lock(&swap_lock);
  380. goto checks;
  381. }
  382. if (unlikely(--latency_ration < 0)) {
  383. cond_resched();
  384. latency_ration = LATENCY_LIMIT;
  385. }
  386. }
  387. spin_lock(&swap_lock);
  388. no_page:
  389. si->flags -= SWP_SCANNING;
  390. return 0;
  391. }
  392. swp_entry_t get_swap_page(void)
  393. {
  394. struct swap_info_struct *si;
  395. pgoff_t offset;
  396. int type, next;
  397. int wrapped = 0;
  398. spin_lock(&swap_lock);
  399. if (nr_swap_pages <= 0)
  400. goto noswap;
  401. nr_swap_pages--;
  402. for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
  403. si = swap_info[type];
  404. next = si->next;
  405. if (next < 0 ||
  406. (!wrapped && si->prio != swap_info[next]->prio)) {
  407. next = swap_list.head;
  408. wrapped++;
  409. }
  410. if (!si->highest_bit)
  411. continue;
  412. if (!(si->flags & SWP_WRITEOK))
  413. continue;
  414. swap_list.next = next;
  415. /* This is called for allocating swap entry for cache */
  416. offset = scan_swap_map(si, SWAP_HAS_CACHE);
  417. if (offset) {
  418. spin_unlock(&swap_lock);
  419. return swp_entry(type, offset);
  420. }
  421. next = swap_list.next;
  422. }
  423. nr_swap_pages++;
  424. noswap:
  425. spin_unlock(&swap_lock);
  426. return (swp_entry_t) {0};
  427. }
  428. /* The only caller of this function is now susupend routine */
  429. swp_entry_t get_swap_page_of_type(int type)
  430. {
  431. struct swap_info_struct *si;
  432. pgoff_t offset;
  433. spin_lock(&swap_lock);
  434. si = swap_info[type];
  435. if (si && (si->flags & SWP_WRITEOK)) {
  436. nr_swap_pages--;
  437. /* This is called for allocating swap entry, not cache */
  438. offset = scan_swap_map(si, 1);
  439. if (offset) {
  440. spin_unlock(&swap_lock);
  441. return swp_entry(type, offset);
  442. }
  443. nr_swap_pages++;
  444. }
  445. spin_unlock(&swap_lock);
  446. return (swp_entry_t) {0};
  447. }
  448. static struct swap_info_struct *swap_info_get(swp_entry_t entry)
  449. {
  450. struct swap_info_struct *p;
  451. unsigned long offset, type;
  452. if (!entry.val)
  453. goto out;
  454. type = swp_type(entry);
  455. if (type >= nr_swapfiles)
  456. goto bad_nofile;
  457. p = swap_info[type];
  458. if (!(p->flags & SWP_USED))
  459. goto bad_device;
  460. offset = swp_offset(entry);
  461. if (offset >= p->max)
  462. goto bad_offset;
  463. if (!p->swap_map[offset])
  464. goto bad_free;
  465. spin_lock(&swap_lock);
  466. return p;
  467. bad_free:
  468. printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
  469. goto out;
  470. bad_offset:
  471. printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
  472. goto out;
  473. bad_device:
  474. printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
  475. goto out;
  476. bad_nofile:
  477. printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
  478. out:
  479. return NULL;
  480. }
  481. static unsigned char swap_entry_free(struct swap_info_struct *p,
  482. swp_entry_t entry, unsigned char usage)
  483. {
  484. unsigned long offset = swp_offset(entry);
  485. unsigned char count;
  486. unsigned char has_cache;
  487. count = p->swap_map[offset];
  488. has_cache = count & SWAP_HAS_CACHE;
  489. count &= ~SWAP_HAS_CACHE;
  490. if (usage == SWAP_HAS_CACHE) {
  491. VM_BUG_ON(!has_cache);
  492. has_cache = 0;
  493. } else if (count == SWAP_MAP_SHMEM) {
  494. /*
  495. * Or we could insist on shmem.c using a special
  496. * swap_shmem_free() and free_shmem_swap_and_cache()...
  497. */
  498. count = 0;
  499. } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
  500. if (count == COUNT_CONTINUED) {
  501. if (swap_count_continued(p, offset, count))
  502. count = SWAP_MAP_MAX | COUNT_CONTINUED;
  503. else
  504. count = SWAP_MAP_MAX;
  505. } else
  506. count--;
  507. }
  508. if (!count)
  509. mem_cgroup_uncharge_swap(entry);
  510. usage = count | has_cache;
  511. p->swap_map[offset] = usage;
  512. /* free if no reference */
  513. if (!usage) {
  514. struct gendisk *disk = p->bdev->bd_disk;
  515. if (offset < p->lowest_bit)
  516. p->lowest_bit = offset;
  517. if (offset > p->highest_bit)
  518. p->highest_bit = offset;
  519. if (swap_list.next >= 0 &&
  520. p->prio > swap_info[swap_list.next]->prio)
  521. swap_list.next = p->type;
  522. nr_swap_pages++;
  523. p->inuse_pages--;
  524. if ((p->flags & SWP_BLKDEV) &&
  525. disk->fops->swap_slot_free_notify)
  526. disk->fops->swap_slot_free_notify(p->bdev, offset);
  527. }
  528. return usage;
  529. }
  530. /*
  531. * Caller has made sure that the swapdevice corresponding to entry
  532. * is still around or has not been recycled.
  533. */
  534. void swap_free(swp_entry_t entry)
  535. {
  536. struct swap_info_struct *p;
  537. p = swap_info_get(entry);
  538. if (p) {
  539. swap_entry_free(p, entry, 1);
  540. spin_unlock(&swap_lock);
  541. }
  542. }
  543. /*
  544. * Called after dropping swapcache to decrease refcnt to swap entries.
  545. */
  546. void swapcache_free(swp_entry_t entry, struct page *page)
  547. {
  548. struct swap_info_struct *p;
  549. unsigned char count;
  550. p = swap_info_get(entry);
  551. if (p) {
  552. count = swap_entry_free(p, entry, SWAP_HAS_CACHE);
  553. if (page)
  554. mem_cgroup_uncharge_swapcache(page, entry, count != 0);
  555. spin_unlock(&swap_lock);
  556. }
  557. }
  558. /*
  559. * How many references to page are currently swapped out?
  560. * This does not give an exact answer when swap count is continued,
  561. * but does include the high COUNT_CONTINUED flag to allow for that.
  562. */
  563. static inline int page_swapcount(struct page *page)
  564. {
  565. int count = 0;
  566. struct swap_info_struct *p;
  567. swp_entry_t entry;
  568. entry.val = page_private(page);
  569. p = swap_info_get(entry);
  570. if (p) {
  571. count = swap_count(p->swap_map[swp_offset(entry)]);
  572. spin_unlock(&swap_lock);
  573. }
  574. return count;
  575. }
  576. /*
  577. * We can write to an anon page without COW if there are no other references
  578. * to it. And as a side-effect, free up its swap: because the old content
  579. * on disk will never be read, and seeking back there to write new content
  580. * later would only waste time away from clustering.
  581. */
  582. int reuse_swap_page(struct page *page)
  583. {
  584. int count;
  585. VM_BUG_ON(!PageLocked(page));
  586. if (unlikely(PageKsm(page)))
  587. return 0;
  588. count = page_mapcount(page);
  589. if (count <= 1 && PageSwapCache(page)) {
  590. count += page_swapcount(page);
  591. if (count == 1 && !PageWriteback(page)) {
  592. delete_from_swap_cache(page);
  593. SetPageDirty(page);
  594. }
  595. }
  596. return count <= 1;
  597. }
  598. /*
  599. * If swap is getting full, or if there are no more mappings of this page,
  600. * then try_to_free_swap is called to free its swap space.
  601. */
  602. int try_to_free_swap(struct page *page)
  603. {
  604. VM_BUG_ON(!PageLocked(page));
  605. if (!PageSwapCache(page))
  606. return 0;
  607. if (PageWriteback(page))
  608. return 0;
  609. if (page_swapcount(page))
  610. return 0;
  611. /*
  612. * Once hibernation has begun to create its image of memory,
  613. * there's a danger that one of the calls to try_to_free_swap()
  614. * - most probably a call from __try_to_reclaim_swap() while
  615. * hibernation is allocating its own swap pages for the image,
  616. * but conceivably even a call from memory reclaim - will free
  617. * the swap from a page which has already been recorded in the
  618. * image as a clean swapcache page, and then reuse its swap for
  619. * another page of the image. On waking from hibernation, the
  620. * original page might be freed under memory pressure, then
  621. * later read back in from swap, now with the wrong data.
  622. *
  623. * Hibernation clears bits from gfp_allowed_mask to prevent
  624. * memory reclaim from writing to disk, so check that here.
  625. */
  626. if (!(gfp_allowed_mask & __GFP_IO))
  627. return 0;
  628. delete_from_swap_cache(page);
  629. SetPageDirty(page);
  630. return 1;
  631. }
  632. /*
  633. * Free the swap entry like above, but also try to
  634. * free the page cache entry if it is the last user.
  635. */
  636. int free_swap_and_cache(swp_entry_t entry)
  637. {
  638. struct swap_info_struct *p;
  639. struct page *page = NULL;
  640. if (non_swap_entry(entry))
  641. return 1;
  642. p = swap_info_get(entry);
  643. if (p) {
  644. if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) {
  645. page = find_get_page(&swapper_space, entry.val);
  646. if (page && !trylock_page(page)) {
  647. page_cache_release(page);
  648. page = NULL;
  649. }
  650. }
  651. spin_unlock(&swap_lock);
  652. }
  653. if (page) {
  654. /*
  655. * Not mapped elsewhere, or swap space full? Free it!
  656. * Also recheck PageSwapCache now page is locked (above).
  657. */
  658. if (PageSwapCache(page) && !PageWriteback(page) &&
  659. (!page_mapped(page) || vm_swap_full())) {
  660. delete_from_swap_cache(page);
  661. SetPageDirty(page);
  662. }
  663. unlock_page(page);
  664. page_cache_release(page);
  665. }
  666. return p != NULL;
  667. }
  668. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  669. /**
  670. * mem_cgroup_count_swap_user - count the user of a swap entry
  671. * @ent: the swap entry to be checked
  672. * @pagep: the pointer for the swap cache page of the entry to be stored
  673. *
  674. * Returns the number of the user of the swap entry. The number is valid only
  675. * for swaps of anonymous pages.
  676. * If the entry is found on swap cache, the page is stored to pagep with
  677. * refcount of it being incremented.
  678. */
  679. int mem_cgroup_count_swap_user(swp_entry_t ent, struct page **pagep)
  680. {
  681. struct page *page;
  682. struct swap_info_struct *p;
  683. int count = 0;
  684. page = find_get_page(&swapper_space, ent.val);
  685. if (page)
  686. count += page_mapcount(page);
  687. p = swap_info_get(ent);
  688. if (p) {
  689. count += swap_count(p->swap_map[swp_offset(ent)]);
  690. spin_unlock(&swap_lock);
  691. }
  692. *pagep = page;
  693. return count;
  694. }
  695. #endif
  696. #ifdef CONFIG_HIBERNATION
  697. /*
  698. * Find the swap type that corresponds to given device (if any).
  699. *
  700. * @offset - number of the PAGE_SIZE-sized block of the device, starting
  701. * from 0, in which the swap header is expected to be located.
  702. *
  703. * This is needed for the suspend to disk (aka swsusp).
  704. */
  705. int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
  706. {
  707. struct block_device *bdev = NULL;
  708. int type;
  709. if (device)
  710. bdev = bdget(device);
  711. spin_lock(&swap_lock);
  712. for (type = 0; type < nr_swapfiles; type++) {
  713. struct swap_info_struct *sis = swap_info[type];
  714. if (!(sis->flags & SWP_WRITEOK))
  715. continue;
  716. if (!bdev) {
  717. if (bdev_p)
  718. *bdev_p = bdgrab(sis->bdev);
  719. spin_unlock(&swap_lock);
  720. return type;
  721. }
  722. if (bdev == sis->bdev) {
  723. struct swap_extent *se = &sis->first_swap_extent;
  724. if (se->start_block == offset) {
  725. if (bdev_p)
  726. *bdev_p = bdgrab(sis->bdev);
  727. spin_unlock(&swap_lock);
  728. bdput(bdev);
  729. return type;
  730. }
  731. }
  732. }
  733. spin_unlock(&swap_lock);
  734. if (bdev)
  735. bdput(bdev);
  736. return -ENODEV;
  737. }
  738. /*
  739. * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
  740. * corresponding to given index in swap_info (swap type).
  741. */
  742. sector_t swapdev_block(int type, pgoff_t offset)
  743. {
  744. struct block_device *bdev;
  745. if ((unsigned int)type >= nr_swapfiles)
  746. return 0;
  747. if (!(swap_info[type]->flags & SWP_WRITEOK))
  748. return 0;
  749. return map_swap_entry(swp_entry(type, offset), &bdev);
  750. }
  751. /*
  752. * Return either the total number of swap pages of given type, or the number
  753. * of free pages of that type (depending on @free)
  754. *
  755. * This is needed for software suspend
  756. */
  757. unsigned int count_swap_pages(int type, int free)
  758. {
  759. unsigned int n = 0;
  760. spin_lock(&swap_lock);
  761. if ((unsigned int)type < nr_swapfiles) {
  762. struct swap_info_struct *sis = swap_info[type];
  763. if (sis->flags & SWP_WRITEOK) {
  764. n = sis->pages;
  765. if (free)
  766. n -= sis->inuse_pages;
  767. }
  768. }
  769. spin_unlock(&swap_lock);
  770. return n;
  771. }
  772. #endif /* CONFIG_HIBERNATION */
  773. /*
  774. * No need to decide whether this PTE shares the swap entry with others,
  775. * just let do_wp_page work it out if a write is requested later - to
  776. * force COW, vm_page_prot omits write permission from any private vma.
  777. */
  778. static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
  779. unsigned long addr, swp_entry_t entry, struct page *page)
  780. {
  781. struct mem_cgroup *ptr = NULL;
  782. spinlock_t *ptl;
  783. pte_t *pte;
  784. int ret = 1;
  785. if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
  786. ret = -ENOMEM;
  787. goto out_nolock;
  788. }
  789. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  790. if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
  791. if (ret > 0)
  792. mem_cgroup_cancel_charge_swapin(ptr);
  793. ret = 0;
  794. goto out;
  795. }
  796. dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
  797. inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
  798. get_page(page);
  799. set_pte_at(vma->vm_mm, addr, pte,
  800. pte_mkold(mk_pte(page, vma->vm_page_prot)));
  801. page_add_anon_rmap(page, vma, addr);
  802. mem_cgroup_commit_charge_swapin(page, ptr);
  803. swap_free(entry);
  804. /*
  805. * Move the page to the active list so it is not
  806. * immediately swapped out again after swapon.
  807. */
  808. activate_page(page);
  809. out:
  810. pte_unmap_unlock(pte, ptl);
  811. out_nolock:
  812. return ret;
  813. }
  814. static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  815. unsigned long addr, unsigned long end,
  816. swp_entry_t entry, struct page *page)
  817. {
  818. pte_t swp_pte = swp_entry_to_pte(entry);
  819. pte_t *pte;
  820. int ret = 0;
  821. /*
  822. * We don't actually need pte lock while scanning for swp_pte: since
  823. * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
  824. * page table while we're scanning; though it could get zapped, and on
  825. * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
  826. * of unmatched parts which look like swp_pte, so unuse_pte must
  827. * recheck under pte lock. Scanning without pte lock lets it be
  828. * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
  829. */
  830. pte = pte_offset_map(pmd, addr);
  831. do {
  832. /*
  833. * swapoff spends a _lot_ of time in this loop!
  834. * Test inline before going to call unuse_pte.
  835. */
  836. if (unlikely(pte_same(*pte, swp_pte))) {
  837. pte_unmap(pte);
  838. ret = unuse_pte(vma, pmd, addr, entry, page);
  839. if (ret)
  840. goto out;
  841. pte = pte_offset_map(pmd, addr);
  842. }
  843. } while (pte++, addr += PAGE_SIZE, addr != end);
  844. pte_unmap(pte - 1);
  845. out:
  846. return ret;
  847. }
  848. static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  849. unsigned long addr, unsigned long end,
  850. swp_entry_t entry, struct page *page)
  851. {
  852. pmd_t *pmd;
  853. unsigned long next;
  854. int ret;
  855. pmd = pmd_offset(pud, addr);
  856. do {
  857. next = pmd_addr_end(addr, end);
  858. if (pmd_none_or_clear_bad(pmd))
  859. continue;
  860. ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
  861. if (ret)
  862. return ret;
  863. } while (pmd++, addr = next, addr != end);
  864. return 0;
  865. }
  866. static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  867. unsigned long addr, unsigned long end,
  868. swp_entry_t entry, struct page *page)
  869. {
  870. pud_t *pud;
  871. unsigned long next;
  872. int ret;
  873. pud = pud_offset(pgd, addr);
  874. do {
  875. next = pud_addr_end(addr, end);
  876. if (pud_none_or_clear_bad(pud))
  877. continue;
  878. ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
  879. if (ret)
  880. return ret;
  881. } while (pud++, addr = next, addr != end);
  882. return 0;
  883. }
  884. static int unuse_vma(struct vm_area_struct *vma,
  885. swp_entry_t entry, struct page *page)
  886. {
  887. pgd_t *pgd;
  888. unsigned long addr, end, next;
  889. int ret;
  890. if (page_anon_vma(page)) {
  891. addr = page_address_in_vma(page, vma);
  892. if (addr == -EFAULT)
  893. return 0;
  894. else
  895. end = addr + PAGE_SIZE;
  896. } else {
  897. addr = vma->vm_start;
  898. end = vma->vm_end;
  899. }
  900. pgd = pgd_offset(vma->vm_mm, addr);
  901. do {
  902. next = pgd_addr_end(addr, end);
  903. if (pgd_none_or_clear_bad(pgd))
  904. continue;
  905. ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
  906. if (ret)
  907. return ret;
  908. } while (pgd++, addr = next, addr != end);
  909. return 0;
  910. }
  911. static int unuse_mm(struct mm_struct *mm,
  912. swp_entry_t entry, struct page *page)
  913. {
  914. struct vm_area_struct *vma;
  915. int ret = 0;
  916. if (!down_read_trylock(&mm->mmap_sem)) {
  917. /*
  918. * Activate page so shrink_inactive_list is unlikely to unmap
  919. * its ptes while lock is dropped, so swapoff can make progress.
  920. */
  921. activate_page(page);
  922. unlock_page(page);
  923. down_read(&mm->mmap_sem);
  924. lock_page(page);
  925. }
  926. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  927. if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
  928. break;
  929. }
  930. up_read(&mm->mmap_sem);
  931. return (ret < 0)? ret: 0;
  932. }
  933. /*
  934. * Scan swap_map from current position to next entry still in use.
  935. * Recycle to start on reaching the end, returning 0 when empty.
  936. */
  937. static unsigned int find_next_to_unuse(struct swap_info_struct *si,
  938. unsigned int prev)
  939. {
  940. unsigned int max = si->max;
  941. unsigned int i = prev;
  942. unsigned char count;
  943. /*
  944. * No need for swap_lock here: we're just looking
  945. * for whether an entry is in use, not modifying it; false
  946. * hits are okay, and sys_swapoff() has already prevented new
  947. * allocations from this area (while holding swap_lock).
  948. */
  949. for (;;) {
  950. if (++i >= max) {
  951. if (!prev) {
  952. i = 0;
  953. break;
  954. }
  955. /*
  956. * No entries in use at top of swap_map,
  957. * loop back to start and recheck there.
  958. */
  959. max = prev + 1;
  960. prev = 0;
  961. i = 1;
  962. }
  963. count = si->swap_map[i];
  964. if (count && swap_count(count) != SWAP_MAP_BAD)
  965. break;
  966. }
  967. return i;
  968. }
  969. /*
  970. * We completely avoid races by reading each swap page in advance,
  971. * and then search for the process using it. All the necessary
  972. * page table adjustments can then be made atomically.
  973. */
  974. static int try_to_unuse(unsigned int type)
  975. {
  976. struct swap_info_struct *si = swap_info[type];
  977. struct mm_struct *start_mm;
  978. unsigned char *swap_map;
  979. unsigned char swcount;
  980. struct page *page;
  981. swp_entry_t entry;
  982. unsigned int i = 0;
  983. int retval = 0;
  984. /*
  985. * When searching mms for an entry, a good strategy is to
  986. * start at the first mm we freed the previous entry from
  987. * (though actually we don't notice whether we or coincidence
  988. * freed the entry). Initialize this start_mm with a hold.
  989. *
  990. * A simpler strategy would be to start at the last mm we
  991. * freed the previous entry from; but that would take less
  992. * advantage of mmlist ordering, which clusters forked mms
  993. * together, child after parent. If we race with dup_mmap(), we
  994. * prefer to resolve parent before child, lest we miss entries
  995. * duplicated after we scanned child: using last mm would invert
  996. * that.
  997. */
  998. start_mm = &init_mm;
  999. atomic_inc(&init_mm.mm_users);
  1000. /*
  1001. * Keep on scanning until all entries have gone. Usually,
  1002. * one pass through swap_map is enough, but not necessarily:
  1003. * there are races when an instance of an entry might be missed.
  1004. */
  1005. while ((i = find_next_to_unuse(si, i)) != 0) {
  1006. if (signal_pending(current)) {
  1007. retval = -EINTR;
  1008. break;
  1009. }
  1010. /*
  1011. * Get a page for the entry, using the existing swap
  1012. * cache page if there is one. Otherwise, get a clean
  1013. * page and read the swap into it.
  1014. */
  1015. swap_map = &si->swap_map[i];
  1016. entry = swp_entry(type, i);
  1017. page = read_swap_cache_async(entry,
  1018. GFP_HIGHUSER_MOVABLE, NULL, 0);
  1019. if (!page) {
  1020. /*
  1021. * Either swap_duplicate() failed because entry
  1022. * has been freed independently, and will not be
  1023. * reused since sys_swapoff() already disabled
  1024. * allocation from here, or alloc_page() failed.
  1025. */
  1026. if (!*swap_map)
  1027. continue;
  1028. retval = -ENOMEM;
  1029. break;
  1030. }
  1031. /*
  1032. * Don't hold on to start_mm if it looks like exiting.
  1033. */
  1034. if (atomic_read(&start_mm->mm_users) == 1) {
  1035. mmput(start_mm);
  1036. start_mm = &init_mm;
  1037. atomic_inc(&init_mm.mm_users);
  1038. }
  1039. /*
  1040. * Wait for and lock page. When do_swap_page races with
  1041. * try_to_unuse, do_swap_page can handle the fault much
  1042. * faster than try_to_unuse can locate the entry. This
  1043. * apparently redundant "wait_on_page_locked" lets try_to_unuse
  1044. * defer to do_swap_page in such a case - in some tests,
  1045. * do_swap_page and try_to_unuse repeatedly compete.
  1046. */
  1047. wait_on_page_locked(page);
  1048. wait_on_page_writeback(page);
  1049. lock_page(page);
  1050. wait_on_page_writeback(page);
  1051. /*
  1052. * Remove all references to entry.
  1053. */
  1054. swcount = *swap_map;
  1055. if (swap_count(swcount) == SWAP_MAP_SHMEM) {
  1056. retval = shmem_unuse(entry, page);
  1057. /* page has already been unlocked and released */
  1058. if (retval < 0)
  1059. break;
  1060. continue;
  1061. }
  1062. if (swap_count(swcount) && start_mm != &init_mm)
  1063. retval = unuse_mm(start_mm, entry, page);
  1064. if (swap_count(*swap_map)) {
  1065. int set_start_mm = (*swap_map >= swcount);
  1066. struct list_head *p = &start_mm->mmlist;
  1067. struct mm_struct *new_start_mm = start_mm;
  1068. struct mm_struct *prev_mm = start_mm;
  1069. struct mm_struct *mm;
  1070. atomic_inc(&new_start_mm->mm_users);
  1071. atomic_inc(&prev_mm->mm_users);
  1072. spin_lock(&mmlist_lock);
  1073. while (swap_count(*swap_map) && !retval &&
  1074. (p = p->next) != &start_mm->mmlist) {
  1075. mm = list_entry(p, struct mm_struct, mmlist);
  1076. if (!atomic_inc_not_zero(&mm->mm_users))
  1077. continue;
  1078. spin_unlock(&mmlist_lock);
  1079. mmput(prev_mm);
  1080. prev_mm = mm;
  1081. cond_resched();
  1082. swcount = *swap_map;
  1083. if (!swap_count(swcount)) /* any usage ? */
  1084. ;
  1085. else if (mm == &init_mm)
  1086. set_start_mm = 1;
  1087. else
  1088. retval = unuse_mm(mm, entry, page);
  1089. if (set_start_mm && *swap_map < swcount) {
  1090. mmput(new_start_mm);
  1091. atomic_inc(&mm->mm_users);
  1092. new_start_mm = mm;
  1093. set_start_mm = 0;
  1094. }
  1095. spin_lock(&mmlist_lock);
  1096. }
  1097. spin_unlock(&mmlist_lock);
  1098. mmput(prev_mm);
  1099. mmput(start_mm);
  1100. start_mm = new_start_mm;
  1101. }
  1102. if (retval) {
  1103. unlock_page(page);
  1104. page_cache_release(page);
  1105. break;
  1106. }
  1107. /*
  1108. * If a reference remains (rare), we would like to leave
  1109. * the page in the swap cache; but try_to_unmap could
  1110. * then re-duplicate the entry once we drop page lock,
  1111. * so we might loop indefinitely; also, that page could
  1112. * not be swapped out to other storage meanwhile. So:
  1113. * delete from cache even if there's another reference,
  1114. * after ensuring that the data has been saved to disk -
  1115. * since if the reference remains (rarer), it will be
  1116. * read from disk into another page. Splitting into two
  1117. * pages would be incorrect if swap supported "shared
  1118. * private" pages, but they are handled by tmpfs files.
  1119. *
  1120. * Given how unuse_vma() targets one particular offset
  1121. * in an anon_vma, once the anon_vma has been determined,
  1122. * this splitting happens to be just what is needed to
  1123. * handle where KSM pages have been swapped out: re-reading
  1124. * is unnecessarily slow, but we can fix that later on.
  1125. */
  1126. if (swap_count(*swap_map) &&
  1127. PageDirty(page) && PageSwapCache(page)) {
  1128. struct writeback_control wbc = {
  1129. .sync_mode = WB_SYNC_NONE,
  1130. };
  1131. swap_writepage(page, &wbc);
  1132. lock_page(page);
  1133. wait_on_page_writeback(page);
  1134. }
  1135. /*
  1136. * It is conceivable that a racing task removed this page from
  1137. * swap cache just before we acquired the page lock at the top,
  1138. * or while we dropped it in unuse_mm(). The page might even
  1139. * be back in swap cache on another swap area: that we must not
  1140. * delete, since it may not have been written out to swap yet.
  1141. */
  1142. if (PageSwapCache(page) &&
  1143. likely(page_private(page) == entry.val))
  1144. delete_from_swap_cache(page);
  1145. /*
  1146. * So we could skip searching mms once swap count went
  1147. * to 1, we did not mark any present ptes as dirty: must
  1148. * mark page dirty so shrink_page_list will preserve it.
  1149. */
  1150. SetPageDirty(page);
  1151. unlock_page(page);
  1152. page_cache_release(page);
  1153. /*
  1154. * Make sure that we aren't completely killing
  1155. * interactive performance.
  1156. */
  1157. cond_resched();
  1158. }
  1159. mmput(start_mm);
  1160. return retval;
  1161. }
  1162. /*
  1163. * After a successful try_to_unuse, if no swap is now in use, we know
  1164. * we can empty the mmlist. swap_lock must be held on entry and exit.
  1165. * Note that mmlist_lock nests inside swap_lock, and an mm must be
  1166. * added to the mmlist just after page_duplicate - before would be racy.
  1167. */
  1168. static void drain_mmlist(void)
  1169. {
  1170. struct list_head *p, *next;
  1171. unsigned int type;
  1172. for (type = 0; type < nr_swapfiles; type++)
  1173. if (swap_info[type]->inuse_pages)
  1174. return;
  1175. spin_lock(&mmlist_lock);
  1176. list_for_each_safe(p, next, &init_mm.mmlist)
  1177. list_del_init(p);
  1178. spin_unlock(&mmlist_lock);
  1179. }
  1180. /*
  1181. * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
  1182. * corresponds to page offset for the specified swap entry.
  1183. * Note that the type of this function is sector_t, but it returns page offset
  1184. * into the bdev, not sector offset.
  1185. */
  1186. static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
  1187. {
  1188. struct swap_info_struct *sis;
  1189. struct swap_extent *start_se;
  1190. struct swap_extent *se;
  1191. pgoff_t offset;
  1192. sis = swap_info[swp_type(entry)];
  1193. *bdev = sis->bdev;
  1194. offset = swp_offset(entry);
  1195. start_se = sis->curr_swap_extent;
  1196. se = start_se;
  1197. for ( ; ; ) {
  1198. struct list_head *lh;
  1199. if (se->start_page <= offset &&
  1200. offset < (se->start_page + se->nr_pages)) {
  1201. return se->start_block + (offset - se->start_page);
  1202. }
  1203. lh = se->list.next;
  1204. se = list_entry(lh, struct swap_extent, list);
  1205. sis->curr_swap_extent = se;
  1206. BUG_ON(se == start_se); /* It *must* be present */
  1207. }
  1208. }
  1209. /*
  1210. * Returns the page offset into bdev for the specified page's swap entry.
  1211. */
  1212. sector_t map_swap_page(struct page *page, struct block_device **bdev)
  1213. {
  1214. swp_entry_t entry;
  1215. entry.val = page_private(page);
  1216. return map_swap_entry(entry, bdev);
  1217. }
  1218. /*
  1219. * Free all of a swapdev's extent information
  1220. */
  1221. static void destroy_swap_extents(struct swap_info_struct *sis)
  1222. {
  1223. while (!list_empty(&sis->first_swap_extent.list)) {
  1224. struct swap_extent *se;
  1225. se = list_entry(sis->first_swap_extent.list.next,
  1226. struct swap_extent, list);
  1227. list_del(&se->list);
  1228. kfree(se);
  1229. }
  1230. }
  1231. /*
  1232. * Add a block range (and the corresponding page range) into this swapdev's
  1233. * extent list. The extent list is kept sorted in page order.
  1234. *
  1235. * This function rather assumes that it is called in ascending page order.
  1236. */
  1237. static int
  1238. add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
  1239. unsigned long nr_pages, sector_t start_block)
  1240. {
  1241. struct swap_extent *se;
  1242. struct swap_extent *new_se;
  1243. struct list_head *lh;
  1244. if (start_page == 0) {
  1245. se = &sis->first_swap_extent;
  1246. sis->curr_swap_extent = se;
  1247. se->start_page = 0;
  1248. se->nr_pages = nr_pages;
  1249. se->start_block = start_block;
  1250. return 1;
  1251. } else {
  1252. lh = sis->first_swap_extent.list.prev; /* Highest extent */
  1253. se = list_entry(lh, struct swap_extent, list);
  1254. BUG_ON(se->start_page + se->nr_pages != start_page);
  1255. if (se->start_block + se->nr_pages == start_block) {
  1256. /* Merge it */
  1257. se->nr_pages += nr_pages;
  1258. return 0;
  1259. }
  1260. }
  1261. /*
  1262. * No merge. Insert a new extent, preserving ordering.
  1263. */
  1264. new_se = kmalloc(sizeof(*se), GFP_KERNEL);
  1265. if (new_se == NULL)
  1266. return -ENOMEM;
  1267. new_se->start_page = start_page;
  1268. new_se->nr_pages = nr_pages;
  1269. new_se->start_block = start_block;
  1270. list_add_tail(&new_se->list, &sis->first_swap_extent.list);
  1271. return 1;
  1272. }
  1273. /*
  1274. * A `swap extent' is a simple thing which maps a contiguous range of pages
  1275. * onto a contiguous range of disk blocks. An ordered list of swap extents
  1276. * is built at swapon time and is then used at swap_writepage/swap_readpage
  1277. * time for locating where on disk a page belongs.
  1278. *
  1279. * If the swapfile is an S_ISBLK block device, a single extent is installed.
  1280. * This is done so that the main operating code can treat S_ISBLK and S_ISREG
  1281. * swap files identically.
  1282. *
  1283. * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
  1284. * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
  1285. * swapfiles are handled *identically* after swapon time.
  1286. *
  1287. * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
  1288. * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
  1289. * some stray blocks are found which do not fall within the PAGE_SIZE alignment
  1290. * requirements, they are simply tossed out - we will never use those blocks
  1291. * for swapping.
  1292. *
  1293. * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
  1294. * prevents root from shooting her foot off by ftruncating an in-use swapfile,
  1295. * which will scribble on the fs.
  1296. *
  1297. * The amount of disk space which a single swap extent represents varies.
  1298. * Typically it is in the 1-4 megabyte range. So we can have hundreds of
  1299. * extents in the list. To avoid much list walking, we cache the previous
  1300. * search location in `curr_swap_extent', and start new searches from there.
  1301. * This is extremely effective. The average number of iterations in
  1302. * map_swap_page() has been measured at about 0.3 per page. - akpm.
  1303. */
  1304. static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
  1305. {
  1306. struct inode *inode;
  1307. unsigned blocks_per_page;
  1308. unsigned long page_no;
  1309. unsigned blkbits;
  1310. sector_t probe_block;
  1311. sector_t last_block;
  1312. sector_t lowest_block = -1;
  1313. sector_t highest_block = 0;
  1314. int nr_extents = 0;
  1315. int ret;
  1316. inode = sis->swap_file->f_mapping->host;
  1317. if (S_ISBLK(inode->i_mode)) {
  1318. ret = add_swap_extent(sis, 0, sis->max, 0);
  1319. *span = sis->pages;
  1320. goto out;
  1321. }
  1322. blkbits = inode->i_blkbits;
  1323. blocks_per_page = PAGE_SIZE >> blkbits;
  1324. /*
  1325. * Map all the blocks into the extent list. This code doesn't try
  1326. * to be very smart.
  1327. */
  1328. probe_block = 0;
  1329. page_no = 0;
  1330. last_block = i_size_read(inode) >> blkbits;
  1331. while ((probe_block + blocks_per_page) <= last_block &&
  1332. page_no < sis->max) {
  1333. unsigned block_in_page;
  1334. sector_t first_block;
  1335. first_block = bmap(inode, probe_block);
  1336. if (first_block == 0)
  1337. goto bad_bmap;
  1338. /*
  1339. * It must be PAGE_SIZE aligned on-disk
  1340. */
  1341. if (first_block & (blocks_per_page - 1)) {
  1342. probe_block++;
  1343. goto reprobe;
  1344. }
  1345. for (block_in_page = 1; block_in_page < blocks_per_page;
  1346. block_in_page++) {
  1347. sector_t block;
  1348. block = bmap(inode, probe_block + block_in_page);
  1349. if (block == 0)
  1350. goto bad_bmap;
  1351. if (block != first_block + block_in_page) {
  1352. /* Discontiguity */
  1353. probe_block++;
  1354. goto reprobe;
  1355. }
  1356. }
  1357. first_block >>= (PAGE_SHIFT - blkbits);
  1358. if (page_no) { /* exclude the header page */
  1359. if (first_block < lowest_block)
  1360. lowest_block = first_block;
  1361. if (first_block > highest_block)
  1362. highest_block = first_block;
  1363. }
  1364. /*
  1365. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  1366. */
  1367. ret = add_swap_extent(sis, page_no, 1, first_block);
  1368. if (ret < 0)
  1369. goto out;
  1370. nr_extents += ret;
  1371. page_no++;
  1372. probe_block += blocks_per_page;
  1373. reprobe:
  1374. continue;
  1375. }
  1376. ret = nr_extents;
  1377. *span = 1 + highest_block - lowest_block;
  1378. if (page_no == 0)
  1379. page_no = 1; /* force Empty message */
  1380. sis->max = page_no;
  1381. sis->pages = page_no - 1;
  1382. sis->highest_bit = page_no - 1;
  1383. out:
  1384. return ret;
  1385. bad_bmap:
  1386. printk(KERN_ERR "swapon: swapfile has holes\n");
  1387. ret = -EINVAL;
  1388. goto out;
  1389. }
  1390. SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
  1391. {
  1392. struct swap_info_struct *p = NULL;
  1393. unsigned char *swap_map;
  1394. struct file *swap_file, *victim;
  1395. struct address_space *mapping;
  1396. struct inode *inode;
  1397. char *pathname;
  1398. int i, type, prev;
  1399. int err;
  1400. if (!capable(CAP_SYS_ADMIN))
  1401. return -EPERM;
  1402. pathname = getname(specialfile);
  1403. err = PTR_ERR(pathname);
  1404. if (IS_ERR(pathname))
  1405. goto out;
  1406. victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
  1407. putname(pathname);
  1408. err = PTR_ERR(victim);
  1409. if (IS_ERR(victim))
  1410. goto out;
  1411. mapping = victim->f_mapping;
  1412. prev = -1;
  1413. spin_lock(&swap_lock);
  1414. for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
  1415. p = swap_info[type];
  1416. if (p->flags & SWP_WRITEOK) {
  1417. if (p->swap_file->f_mapping == mapping)
  1418. break;
  1419. }
  1420. prev = type;
  1421. }
  1422. if (type < 0) {
  1423. err = -EINVAL;
  1424. spin_unlock(&swap_lock);
  1425. goto out_dput;
  1426. }
  1427. if (!security_vm_enough_memory(p->pages))
  1428. vm_unacct_memory(p->pages);
  1429. else {
  1430. err = -ENOMEM;
  1431. spin_unlock(&swap_lock);
  1432. goto out_dput;
  1433. }
  1434. if (prev < 0)
  1435. swap_list.head = p->next;
  1436. else
  1437. swap_info[prev]->next = p->next;
  1438. if (type == swap_list.next) {
  1439. /* just pick something that's safe... */
  1440. swap_list.next = swap_list.head;
  1441. }
  1442. if (p->prio < 0) {
  1443. for (i = p->next; i >= 0; i = swap_info[i]->next)
  1444. swap_info[i]->prio = p->prio--;
  1445. least_priority++;
  1446. }
  1447. nr_swap_pages -= p->pages;
  1448. total_swap_pages -= p->pages;
  1449. p->flags &= ~SWP_WRITEOK;
  1450. spin_unlock(&swap_lock);
  1451. current->flags |= PF_OOM_ORIGIN;
  1452. err = try_to_unuse(type);
  1453. current->flags &= ~PF_OOM_ORIGIN;
  1454. if (err) {
  1455. /* re-insert swap space back into swap_list */
  1456. spin_lock(&swap_lock);
  1457. if (p->prio < 0)
  1458. p->prio = --least_priority;
  1459. prev = -1;
  1460. for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
  1461. if (p->prio >= swap_info[i]->prio)
  1462. break;
  1463. prev = i;
  1464. }
  1465. p->next = i;
  1466. if (prev < 0)
  1467. swap_list.head = swap_list.next = type;
  1468. else
  1469. swap_info[prev]->next = type;
  1470. nr_swap_pages += p->pages;
  1471. total_swap_pages += p->pages;
  1472. p->flags |= SWP_WRITEOK;
  1473. spin_unlock(&swap_lock);
  1474. goto out_dput;
  1475. }
  1476. /* wait for any unplug function to finish */
  1477. down_write(&swap_unplug_sem);
  1478. up_write(&swap_unplug_sem);
  1479. destroy_swap_extents(p);
  1480. if (p->flags & SWP_CONTINUED)
  1481. free_swap_count_continuations(p);
  1482. mutex_lock(&swapon_mutex);
  1483. spin_lock(&swap_lock);
  1484. drain_mmlist();
  1485. /* wait for anyone still in scan_swap_map */
  1486. p->highest_bit = 0; /* cuts scans short */
  1487. while (p->flags >= SWP_SCANNING) {
  1488. spin_unlock(&swap_lock);
  1489. schedule_timeout_uninterruptible(1);
  1490. spin_lock(&swap_lock);
  1491. }
  1492. swap_file = p->swap_file;
  1493. p->swap_file = NULL;
  1494. p->max = 0;
  1495. swap_map = p->swap_map;
  1496. p->swap_map = NULL;
  1497. p->flags = 0;
  1498. spin_unlock(&swap_lock);
  1499. mutex_unlock(&swapon_mutex);
  1500. vfree(swap_map);
  1501. /* Destroy swap account informatin */
  1502. swap_cgroup_swapoff(type);
  1503. inode = mapping->host;
  1504. if (S_ISBLK(inode->i_mode)) {
  1505. struct block_device *bdev = I_BDEV(inode);
  1506. set_blocksize(bdev, p->old_block_size);
  1507. bd_release(bdev);
  1508. } else {
  1509. mutex_lock(&inode->i_mutex);
  1510. inode->i_flags &= ~S_SWAPFILE;
  1511. mutex_unlock(&inode->i_mutex);
  1512. }
  1513. filp_close(swap_file, NULL);
  1514. err = 0;
  1515. atomic_inc(&proc_poll_event);
  1516. wake_up_interruptible(&proc_poll_wait);
  1517. out_dput:
  1518. filp_close(victim, NULL);
  1519. out:
  1520. return err;
  1521. }
  1522. #ifdef CONFIG_PROC_FS
  1523. struct proc_swaps {
  1524. struct seq_file seq;
  1525. int event;
  1526. };
  1527. static unsigned swaps_poll(struct file *file, poll_table *wait)
  1528. {
  1529. struct proc_swaps *s = file->private_data;
  1530. poll_wait(file, &proc_poll_wait, wait);
  1531. if (s->event != atomic_read(&proc_poll_event)) {
  1532. s->event = atomic_read(&proc_poll_event);
  1533. return POLLIN | POLLRDNORM | POLLERR | POLLPRI;
  1534. }
  1535. return POLLIN | POLLRDNORM;
  1536. }
  1537. /* iterator */
  1538. static void *swap_start(struct seq_file *swap, loff_t *pos)
  1539. {
  1540. struct swap_info_struct *si;
  1541. int type;
  1542. loff_t l = *pos;
  1543. mutex_lock(&swapon_mutex);
  1544. if (!l)
  1545. return SEQ_START_TOKEN;
  1546. for (type = 0; type < nr_swapfiles; type++) {
  1547. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  1548. si = swap_info[type];
  1549. if (!(si->flags & SWP_USED) || !si->swap_map)
  1550. continue;
  1551. if (!--l)
  1552. return si;
  1553. }
  1554. return NULL;
  1555. }
  1556. static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
  1557. {
  1558. struct swap_info_struct *si = v;
  1559. int type;
  1560. if (v == SEQ_START_TOKEN)
  1561. type = 0;
  1562. else
  1563. type = si->type + 1;
  1564. for (; type < nr_swapfiles; type++) {
  1565. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  1566. si = swap_info[type];
  1567. if (!(si->flags & SWP_USED) || !si->swap_map)
  1568. continue;
  1569. ++*pos;
  1570. return si;
  1571. }
  1572. return NULL;
  1573. }
  1574. static void swap_stop(struct seq_file *swap, void *v)
  1575. {
  1576. mutex_unlock(&swapon_mutex);
  1577. }
  1578. static int swap_show(struct seq_file *swap, void *v)
  1579. {
  1580. struct swap_info_struct *si = v;
  1581. struct file *file;
  1582. int len;
  1583. if (si == SEQ_START_TOKEN) {
  1584. seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
  1585. return 0;
  1586. }
  1587. file = si->swap_file;
  1588. len = seq_path(swap, &file->f_path, " \t\n\\");
  1589. seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
  1590. len < 40 ? 40 - len : 1, " ",
  1591. S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
  1592. "partition" : "file\t",
  1593. si->pages << (PAGE_SHIFT - 10),
  1594. si->inuse_pages << (PAGE_SHIFT - 10),
  1595. si->prio);
  1596. return 0;
  1597. }
  1598. static const struct seq_operations swaps_op = {
  1599. .start = swap_start,
  1600. .next = swap_next,
  1601. .stop = swap_stop,
  1602. .show = swap_show
  1603. };
  1604. static int swaps_open(struct inode *inode, struct file *file)
  1605. {
  1606. struct proc_swaps *s;
  1607. int ret;
  1608. s = kmalloc(sizeof(struct proc_swaps), GFP_KERNEL);
  1609. if (!s)
  1610. return -ENOMEM;
  1611. file->private_data = s;
  1612. ret = seq_open(file, &swaps_op);
  1613. if (ret) {
  1614. kfree(s);
  1615. return ret;
  1616. }
  1617. s->seq.private = s;
  1618. s->event = atomic_read(&proc_poll_event);
  1619. return ret;
  1620. }
  1621. static const struct file_operations proc_swaps_operations = {
  1622. .open = swaps_open,
  1623. .read = seq_read,
  1624. .llseek = seq_lseek,
  1625. .release = seq_release,
  1626. .poll = swaps_poll,
  1627. };
  1628. static int __init procswaps_init(void)
  1629. {
  1630. proc_create("swaps", 0, NULL, &proc_swaps_operations);
  1631. return 0;
  1632. }
  1633. __initcall(procswaps_init);
  1634. #endif /* CONFIG_PROC_FS */
  1635. #ifdef MAX_SWAPFILES_CHECK
  1636. static int __init max_swapfiles_check(void)
  1637. {
  1638. MAX_SWAPFILES_CHECK();
  1639. return 0;
  1640. }
  1641. late_initcall(max_swapfiles_check);
  1642. #endif
  1643. /*
  1644. * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
  1645. *
  1646. * The swapon system call
  1647. */
  1648. SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
  1649. {
  1650. struct swap_info_struct *p;
  1651. char *name = NULL;
  1652. struct block_device *bdev = NULL;
  1653. struct file *swap_file = NULL;
  1654. struct address_space *mapping;
  1655. unsigned int type;
  1656. int i, prev;
  1657. int error;
  1658. union swap_header *swap_header;
  1659. unsigned int nr_good_pages;
  1660. int nr_extents = 0;
  1661. sector_t span;
  1662. unsigned long maxpages;
  1663. unsigned long swapfilepages;
  1664. unsigned char *swap_map = NULL;
  1665. struct page *page = NULL;
  1666. struct inode *inode = NULL;
  1667. int did_down = 0;
  1668. if (!capable(CAP_SYS_ADMIN))
  1669. return -EPERM;
  1670. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1671. if (!p)
  1672. return -ENOMEM;
  1673. spin_lock(&swap_lock);
  1674. for (type = 0; type < nr_swapfiles; type++) {
  1675. if (!(swap_info[type]->flags & SWP_USED))
  1676. break;
  1677. }
  1678. error = -EPERM;
  1679. if (type >= MAX_SWAPFILES) {
  1680. spin_unlock(&swap_lock);
  1681. kfree(p);
  1682. goto out;
  1683. }
  1684. if (type >= nr_swapfiles) {
  1685. p->type = type;
  1686. swap_info[type] = p;
  1687. /*
  1688. * Write swap_info[type] before nr_swapfiles, in case a
  1689. * racing procfs swap_start() or swap_next() is reading them.
  1690. * (We never shrink nr_swapfiles, we never free this entry.)
  1691. */
  1692. smp_wmb();
  1693. nr_swapfiles++;
  1694. } else {
  1695. kfree(p);
  1696. p = swap_info[type];
  1697. /*
  1698. * Do not memset this entry: a racing procfs swap_next()
  1699. * would be relying on p->type to remain valid.
  1700. */
  1701. }
  1702. INIT_LIST_HEAD(&p->first_swap_extent.list);
  1703. p->flags = SWP_USED;
  1704. p->next = -1;
  1705. spin_unlock(&swap_lock);
  1706. name = getname(specialfile);
  1707. error = PTR_ERR(name);
  1708. if (IS_ERR(name)) {
  1709. name = NULL;
  1710. goto bad_swap_2;
  1711. }
  1712. swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
  1713. error = PTR_ERR(swap_file);
  1714. if (IS_ERR(swap_file)) {
  1715. swap_file = NULL;
  1716. goto bad_swap_2;
  1717. }
  1718. p->swap_file = swap_file;
  1719. mapping = swap_file->f_mapping;
  1720. inode = mapping->host;
  1721. error = -EBUSY;
  1722. for (i = 0; i < nr_swapfiles; i++) {
  1723. struct swap_info_struct *q = swap_info[i];
  1724. if (i == type || !q->swap_file)
  1725. continue;
  1726. if (mapping == q->swap_file->f_mapping)
  1727. goto bad_swap;
  1728. }
  1729. error = -EINVAL;
  1730. if (S_ISBLK(inode->i_mode)) {
  1731. bdev = I_BDEV(inode);
  1732. error = bd_claim(bdev, sys_swapon);
  1733. if (error < 0) {
  1734. bdev = NULL;
  1735. error = -EINVAL;
  1736. goto bad_swap;
  1737. }
  1738. p->old_block_size = block_size(bdev);
  1739. error = set_blocksize(bdev, PAGE_SIZE);
  1740. if (error < 0)
  1741. goto bad_swap;
  1742. p->bdev = bdev;
  1743. p->flags |= SWP_BLKDEV;
  1744. } else if (S_ISREG(inode->i_mode)) {
  1745. p->bdev = inode->i_sb->s_bdev;
  1746. mutex_lock(&inode->i_mutex);
  1747. did_down = 1;
  1748. if (IS_SWAPFILE(inode)) {
  1749. error = -EBUSY;
  1750. goto bad_swap;
  1751. }
  1752. } else {
  1753. goto bad_swap;
  1754. }
  1755. swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
  1756. /*
  1757. * Read the swap header.
  1758. */
  1759. if (!mapping->a_ops->readpage) {
  1760. error = -EINVAL;
  1761. goto bad_swap;
  1762. }
  1763. page = read_mapping_page(mapping, 0, swap_file);
  1764. if (IS_ERR(page)) {
  1765. error = PTR_ERR(page);
  1766. goto bad_swap;
  1767. }
  1768. swap_header = kmap(page);
  1769. if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
  1770. printk(KERN_ERR "Unable to find swap-space signature\n");
  1771. error = -EINVAL;
  1772. goto bad_swap;
  1773. }
  1774. /* swap partition endianess hack... */
  1775. if (swab32(swap_header->info.version) == 1) {
  1776. swab32s(&swap_header->info.version);
  1777. swab32s(&swap_header->info.last_page);
  1778. swab32s(&swap_header->info.nr_badpages);
  1779. for (i = 0; i < swap_header->info.nr_badpages; i++)
  1780. swab32s(&swap_header->info.badpages[i]);
  1781. }
  1782. /* Check the swap header's sub-version */
  1783. if (swap_header->info.version != 1) {
  1784. printk(KERN_WARNING
  1785. "Unable to handle swap header version %d\n",
  1786. swap_header->info.version);
  1787. error = -EINVAL;
  1788. goto bad_swap;
  1789. }
  1790. p->lowest_bit = 1;
  1791. p->cluster_next = 1;
  1792. p->cluster_nr = 0;
  1793. /*
  1794. * Find out how many pages are allowed for a single swap
  1795. * device. There are two limiting factors: 1) the number of
  1796. * bits for the swap offset in the swp_entry_t type and
  1797. * 2) the number of bits in the a swap pte as defined by
  1798. * the different architectures. In order to find the
  1799. * largest possible bit mask a swap entry with swap type 0
  1800. * and swap offset ~0UL is created, encoded to a swap pte,
  1801. * decoded to a swp_entry_t again and finally the swap
  1802. * offset is extracted. This will mask all the bits from
  1803. * the initial ~0UL mask that can't be encoded in either
  1804. * the swp_entry_t or the architecture definition of a
  1805. * swap pte.
  1806. */
  1807. maxpages = swp_offset(pte_to_swp_entry(
  1808. swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
  1809. if (maxpages > swap_header->info.last_page) {
  1810. maxpages = swap_header->info.last_page + 1;
  1811. /* p->max is an unsigned int: don't overflow it */
  1812. if ((unsigned int)maxpages == 0)
  1813. maxpages = UINT_MAX;
  1814. }
  1815. p->highest_bit = maxpages - 1;
  1816. error = -EINVAL;
  1817. if (!maxpages)
  1818. goto bad_swap;
  1819. if (swapfilepages && maxpages > swapfilepages) {
  1820. printk(KERN_WARNING
  1821. "Swap area shorter than signature indicates\n");
  1822. goto bad_swap;
  1823. }
  1824. if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
  1825. goto bad_swap;
  1826. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  1827. goto bad_swap;
  1828. /* OK, set up the swap map and apply the bad block list */
  1829. swap_map = vmalloc(maxpages);
  1830. if (!swap_map) {
  1831. error = -ENOMEM;
  1832. goto bad_swap;
  1833. }
  1834. memset(swap_map, 0, maxpages);
  1835. nr_good_pages = maxpages - 1; /* omit header page */
  1836. for (i = 0; i < swap_header->info.nr_badpages; i++) {
  1837. unsigned int page_nr = swap_header->info.badpages[i];
  1838. if (page_nr == 0 || page_nr > swap_header->info.last_page) {
  1839. error = -EINVAL;
  1840. goto bad_swap;
  1841. }
  1842. if (page_nr < maxpages) {
  1843. swap_map[page_nr] = SWAP_MAP_BAD;
  1844. nr_good_pages--;
  1845. }
  1846. }
  1847. error = swap_cgroup_swapon(type, maxpages);
  1848. if (error)
  1849. goto bad_swap;
  1850. if (nr_good_pages) {
  1851. swap_map[0] = SWAP_MAP_BAD;
  1852. p->max = maxpages;
  1853. p->pages = nr_good_pages;
  1854. nr_extents = setup_swap_extents(p, &span);
  1855. if (nr_extents < 0) {
  1856. error = nr_extents;
  1857. goto bad_swap;
  1858. }
  1859. nr_good_pages = p->pages;
  1860. }
  1861. if (!nr_good_pages) {
  1862. printk(KERN_WARNING "Empty swap-file\n");
  1863. error = -EINVAL;
  1864. goto bad_swap;
  1865. }
  1866. if (p->bdev) {
  1867. if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
  1868. p->flags |= SWP_SOLIDSTATE;
  1869. p->cluster_next = 1 + (random32() % p->highest_bit);
  1870. }
  1871. if (discard_swap(p) == 0 && (swap_flags & SWAP_FLAG_DISCARD))
  1872. p->flags |= SWP_DISCARDABLE;
  1873. }
  1874. mutex_lock(&swapon_mutex);
  1875. spin_lock(&swap_lock);
  1876. if (swap_flags & SWAP_FLAG_PREFER)
  1877. p->prio =
  1878. (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
  1879. else
  1880. p->prio = --least_priority;
  1881. p->swap_map = swap_map;
  1882. p->flags |= SWP_WRITEOK;
  1883. nr_swap_pages += nr_good_pages;
  1884. total_swap_pages += nr_good_pages;
  1885. printk(KERN_INFO "Adding %uk swap on %s. "
  1886. "Priority:%d extents:%d across:%lluk %s%s\n",
  1887. nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
  1888. nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
  1889. (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
  1890. (p->flags & SWP_DISCARDABLE) ? "D" : "");
  1891. /* insert swap space into swap_list: */
  1892. prev = -1;
  1893. for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
  1894. if (p->prio >= swap_info[i]->prio)
  1895. break;
  1896. prev = i;
  1897. }
  1898. p->next = i;
  1899. if (prev < 0)
  1900. swap_list.head = swap_list.next = type;
  1901. else
  1902. swap_info[prev]->next = type;
  1903. spin_unlock(&swap_lock);
  1904. mutex_unlock(&swapon_mutex);
  1905. atomic_inc(&proc_poll_event);
  1906. wake_up_interruptible(&proc_poll_wait);
  1907. error = 0;
  1908. goto out;
  1909. bad_swap:
  1910. if (bdev) {
  1911. set_blocksize(bdev, p->old_block_size);
  1912. bd_release(bdev);
  1913. }
  1914. destroy_swap_extents(p);
  1915. swap_cgroup_swapoff(type);
  1916. bad_swap_2:
  1917. spin_lock(&swap_lock);
  1918. p->swap_file = NULL;
  1919. p->flags = 0;
  1920. spin_unlock(&swap_lock);
  1921. vfree(swap_map);
  1922. if (swap_file)
  1923. filp_close(swap_file, NULL);
  1924. out:
  1925. if (page && !IS_ERR(page)) {
  1926. kunmap(page);
  1927. page_cache_release(page);
  1928. }
  1929. if (name)
  1930. putname(name);
  1931. if (did_down) {
  1932. if (!error)
  1933. inode->i_flags |= S_SWAPFILE;
  1934. mutex_unlock(&inode->i_mutex);
  1935. }
  1936. return error;
  1937. }
  1938. void si_swapinfo(struct sysinfo *val)
  1939. {
  1940. unsigned int type;
  1941. unsigned long nr_to_be_unused = 0;
  1942. spin_lock(&swap_lock);
  1943. for (type = 0; type < nr_swapfiles; type++) {
  1944. struct swap_info_struct *si = swap_info[type];
  1945. if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
  1946. nr_to_be_unused += si->inuse_pages;
  1947. }
  1948. val->freeswap = nr_swap_pages + nr_to_be_unused;
  1949. val->totalswap = total_swap_pages + nr_to_be_unused;
  1950. spin_unlock(&swap_lock);
  1951. }
  1952. /*
  1953. * Verify that a swap entry is valid and increment its swap map count.
  1954. *
  1955. * Returns error code in following case.
  1956. * - success -> 0
  1957. * - swp_entry is invalid -> EINVAL
  1958. * - swp_entry is migration entry -> EINVAL
  1959. * - swap-cache reference is requested but there is already one. -> EEXIST
  1960. * - swap-cache reference is requested but the entry is not used. -> ENOENT
  1961. * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
  1962. */
  1963. static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
  1964. {
  1965. struct swap_info_struct *p;
  1966. unsigned long offset, type;
  1967. unsigned char count;
  1968. unsigned char has_cache;
  1969. int err = -EINVAL;
  1970. if (non_swap_entry(entry))
  1971. goto out;
  1972. type = swp_type(entry);
  1973. if (type >= nr_swapfiles)
  1974. goto bad_file;
  1975. p = swap_info[type];
  1976. offset = swp_offset(entry);
  1977. spin_lock(&swap_lock);
  1978. if (unlikely(offset >= p->max))
  1979. goto unlock_out;
  1980. count = p->swap_map[offset];
  1981. has_cache = count & SWAP_HAS_CACHE;
  1982. count &= ~SWAP_HAS_CACHE;
  1983. err = 0;
  1984. if (usage == SWAP_HAS_CACHE) {
  1985. /* set SWAP_HAS_CACHE if there is no cache and entry is used */
  1986. if (!has_cache && count)
  1987. has_cache = SWAP_HAS_CACHE;
  1988. else if (has_cache) /* someone else added cache */
  1989. err = -EEXIST;
  1990. else /* no users remaining */
  1991. err = -ENOENT;
  1992. } else if (count || has_cache) {
  1993. if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
  1994. count += usage;
  1995. else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
  1996. err = -EINVAL;
  1997. else if (swap_count_continued(p, offset, count))
  1998. count = COUNT_CONTINUED;
  1999. else
  2000. err = -ENOMEM;
  2001. } else
  2002. err = -ENOENT; /* unused swap entry */
  2003. p->swap_map[offset] = count | has_cache;
  2004. unlock_out:
  2005. spin_unlock(&swap_lock);
  2006. out:
  2007. return err;
  2008. bad_file:
  2009. printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
  2010. goto out;
  2011. }
  2012. /*
  2013. * Help swapoff by noting that swap entry belongs to shmem/tmpfs
  2014. * (in which case its reference count is never incremented).
  2015. */
  2016. void swap_shmem_alloc(swp_entry_t entry)
  2017. {
  2018. __swap_duplicate(entry, SWAP_MAP_SHMEM);
  2019. }
  2020. /*
  2021. * Increase reference count of swap entry by 1.
  2022. * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
  2023. * but could not be atomically allocated. Returns 0, just as if it succeeded,
  2024. * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
  2025. * might occur if a page table entry has got corrupted.
  2026. */
  2027. int swap_duplicate(swp_entry_t entry)
  2028. {
  2029. int err = 0;
  2030. while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
  2031. err = add_swap_count_continuation(entry, GFP_ATOMIC);
  2032. return err;
  2033. }
  2034. /*
  2035. * @entry: swap entry for which we allocate swap cache.
  2036. *
  2037. * Called when allocating swap cache for existing swap entry,
  2038. * This can return error codes. Returns 0 at success.
  2039. * -EBUSY means there is a swap cache.
  2040. * Note: return code is different from swap_duplicate().
  2041. */
  2042. int swapcache_prepare(swp_entry_t entry)
  2043. {
  2044. return __swap_duplicate(entry, SWAP_HAS_CACHE);
  2045. }
  2046. /*
  2047. * swap_lock prevents swap_map being freed. Don't grab an extra
  2048. * reference on the swaphandle, it doesn't matter if it becomes unused.
  2049. */
  2050. int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
  2051. {
  2052. struct swap_info_struct *si;
  2053. int our_page_cluster = page_cluster;
  2054. pgoff_t target, toff;
  2055. pgoff_t base, end;
  2056. int nr_pages = 0;
  2057. if (!our_page_cluster) /* no readahead */
  2058. return 0;
  2059. si = swap_info[swp_type(entry)];
  2060. target = swp_offset(entry);
  2061. base = (target >> our_page_cluster) << our_page_cluster;
  2062. end = base + (1 << our_page_cluster);
  2063. if (!base) /* first page is swap header */
  2064. base++;
  2065. spin_lock(&swap_lock);
  2066. if (end > si->max) /* don't go beyond end of map */
  2067. end = si->max;
  2068. /* Count contiguous allocated slots above our target */
  2069. for (toff = target; ++toff < end; nr_pages++) {
  2070. /* Don't read in free or bad pages */
  2071. if (!si->swap_map[toff])
  2072. break;
  2073. if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
  2074. break;
  2075. }
  2076. /* Count contiguous allocated slots below our target */
  2077. for (toff = target; --toff >= base; nr_pages++) {
  2078. /* Don't read in free or bad pages */
  2079. if (!si->swap_map[toff])
  2080. break;
  2081. if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
  2082. break;
  2083. }
  2084. spin_unlock(&swap_lock);
  2085. /*
  2086. * Indicate starting offset, and return number of pages to get:
  2087. * if only 1, say 0, since there's then no readahead to be done.
  2088. */
  2089. *offset = ++toff;
  2090. return nr_pages? ++nr_pages: 0;
  2091. }
  2092. /*
  2093. * add_swap_count_continuation - called when a swap count is duplicated
  2094. * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
  2095. * page of the original vmalloc'ed swap_map, to hold the continuation count
  2096. * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called
  2097. * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
  2098. *
  2099. * These continuation pages are seldom referenced: the common paths all work
  2100. * on the original swap_map, only referring to a continuation page when the
  2101. * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
  2102. *
  2103. * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
  2104. * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
  2105. * can be called after dropping locks.
  2106. */
  2107. int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
  2108. {
  2109. struct swap_info_struct *si;
  2110. struct page *head;
  2111. struct page *page;
  2112. struct page *list_page;
  2113. pgoff_t offset;
  2114. unsigned char count;
  2115. /*
  2116. * When debugging, it's easier to use __GFP_ZERO here; but it's better
  2117. * for latency not to zero a page while GFP_ATOMIC and holding locks.
  2118. */
  2119. page = alloc_page(gfp_mask | __GFP_HIGHMEM);
  2120. si = swap_info_get(entry);
  2121. if (!si) {
  2122. /*
  2123. * An acceptable race has occurred since the failing
  2124. * __swap_duplicate(): the swap entry has been freed,
  2125. * perhaps even the whole swap_map cleared for swapoff.
  2126. */
  2127. goto outer;
  2128. }
  2129. offset = swp_offset(entry);
  2130. count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
  2131. if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
  2132. /*
  2133. * The higher the swap count, the more likely it is that tasks
  2134. * will race to add swap count continuation: we need to avoid
  2135. * over-provisioning.
  2136. */
  2137. goto out;
  2138. }
  2139. if (!page) {
  2140. spin_unlock(&swap_lock);
  2141. return -ENOMEM;
  2142. }
  2143. /*
  2144. * We are fortunate that although vmalloc_to_page uses pte_offset_map,
  2145. * no architecture is using highmem pages for kernel pagetables: so it
  2146. * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
  2147. */
  2148. head = vmalloc_to_page(si->swap_map + offset);
  2149. offset &= ~PAGE_MASK;
  2150. /*
  2151. * Page allocation does not initialize the page's lru field,
  2152. * but it does always reset its private field.
  2153. */
  2154. if (!page_private(head)) {
  2155. BUG_ON(count & COUNT_CONTINUED);
  2156. INIT_LIST_HEAD(&head->lru);
  2157. set_page_private(head, SWP_CONTINUED);
  2158. si->flags |= SWP_CONTINUED;
  2159. }
  2160. list_for_each_entry(list_page, &head->lru, lru) {
  2161. unsigned char *map;
  2162. /*
  2163. * If the previous map said no continuation, but we've found
  2164. * a continuation page, free our allocation and use this one.
  2165. */
  2166. if (!(count & COUNT_CONTINUED))
  2167. goto out;
  2168. map = kmap_atomic(list_page, KM_USER0) + offset;
  2169. count = *map;
  2170. kunmap_atomic(map, KM_USER0);
  2171. /*
  2172. * If this continuation count now has some space in it,
  2173. * free our allocation and use this one.
  2174. */
  2175. if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
  2176. goto out;
  2177. }
  2178. list_add_tail(&page->lru, &head->lru);
  2179. page = NULL; /* now it's attached, don't free it */
  2180. out:
  2181. spin_unlock(&swap_lock);
  2182. outer:
  2183. if (page)
  2184. __free_page(page);
  2185. return 0;
  2186. }
  2187. /*
  2188. * swap_count_continued - when the original swap_map count is incremented
  2189. * from SWAP_MAP_MAX, check if there is already a continuation page to carry
  2190. * into, carry if so, or else fail until a new continuation page is allocated;
  2191. * when the original swap_map count is decremented from 0 with continuation,
  2192. * borrow from the continuation and report whether it still holds more.
  2193. * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
  2194. */
  2195. static bool swap_count_continued(struct swap_info_struct *si,
  2196. pgoff_t offset, unsigned char count)
  2197. {
  2198. struct page *head;
  2199. struct page *page;
  2200. unsigned char *map;
  2201. head = vmalloc_to_page(si->swap_map + offset);
  2202. if (page_private(head) != SWP_CONTINUED) {
  2203. BUG_ON(count & COUNT_CONTINUED);
  2204. return false; /* need to add count continuation */
  2205. }
  2206. offset &= ~PAGE_MASK;
  2207. page = list_entry(head->lru.next, struct page, lru);
  2208. map = kmap_atomic(page, KM_USER0) + offset;
  2209. if (count == SWAP_MAP_MAX) /* initial increment from swap_map */
  2210. goto init_map; /* jump over SWAP_CONT_MAX checks */
  2211. if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
  2212. /*
  2213. * Think of how you add 1 to 999
  2214. */
  2215. while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
  2216. kunmap_atomic(map, KM_USER0);
  2217. page = list_entry(page->lru.next, struct page, lru);
  2218. BUG_ON(page == head);
  2219. map = kmap_atomic(page, KM_USER0) + offset;
  2220. }
  2221. if (*map == SWAP_CONT_MAX) {
  2222. kunmap_atomic(map, KM_USER0);
  2223. page = list_entry(page->lru.next, struct page, lru);
  2224. if (page == head)
  2225. return false; /* add count continuation */
  2226. map = kmap_atomic(page, KM_USER0) + offset;
  2227. init_map: *map = 0; /* we didn't zero the page */
  2228. }
  2229. *map += 1;
  2230. kunmap_atomic(map, KM_USER0);
  2231. page = list_entry(page->lru.prev, struct page, lru);
  2232. while (page != head) {
  2233. map = kmap_atomic(page, KM_USER0) + offset;
  2234. *map = COUNT_CONTINUED;
  2235. kunmap_atomic(map, KM_USER0);
  2236. page = list_entry(page->lru.prev, struct page, lru);
  2237. }
  2238. return true; /* incremented */
  2239. } else { /* decrementing */
  2240. /*
  2241. * Think of how you subtract 1 from 1000
  2242. */
  2243. BUG_ON(count != COUNT_CONTINUED);
  2244. while (*map == COUNT_CONTINUED) {
  2245. kunmap_atomic(map, KM_USER0);
  2246. page = list_entry(page->lru.next, struct page, lru);
  2247. BUG_ON(page == head);
  2248. map = kmap_atomic(page, KM_USER0) + offset;
  2249. }
  2250. BUG_ON(*map == 0);
  2251. *map -= 1;
  2252. if (*map == 0)
  2253. count = 0;
  2254. kunmap_atomic(map, KM_USER0);
  2255. page = list_entry(page->lru.prev, struct page, lru);
  2256. while (page != head) {
  2257. map = kmap_atomic(page, KM_USER0) + offset;
  2258. *map = SWAP_CONT_MAX | count;
  2259. count = COUNT_CONTINUED;
  2260. kunmap_atomic(map, KM_USER0);
  2261. page = list_entry(page->lru.prev, struct page, lru);
  2262. }
  2263. return count == COUNT_CONTINUED;
  2264. }
  2265. }
  2266. /*
  2267. * free_swap_count_continuations - swapoff free all the continuation pages
  2268. * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
  2269. */
  2270. static void free_swap_count_continuations(struct swap_info_struct *si)
  2271. {
  2272. pgoff_t offset;
  2273. for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
  2274. struct page *head;
  2275. head = vmalloc_to_page(si->swap_map + offset);
  2276. if (page_private(head)) {
  2277. struct list_head *this, *next;
  2278. list_for_each_safe(this, next, &head->lru) {
  2279. struct page *page;
  2280. page = list_entry(this, struct page, lru);
  2281. list_del(this);
  2282. __free_page(page);
  2283. }
  2284. }
  2285. }
  2286. }