swapfile.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  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 (unlikely(pmd_trans_huge(*pmd)))
  859. continue;
  860. if (pmd_none_or_clear_bad(pmd))
  861. continue;
  862. ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
  863. if (ret)
  864. return ret;
  865. } while (pmd++, addr = next, addr != end);
  866. return 0;
  867. }
  868. static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  869. unsigned long addr, unsigned long end,
  870. swp_entry_t entry, struct page *page)
  871. {
  872. pud_t *pud;
  873. unsigned long next;
  874. int ret;
  875. pud = pud_offset(pgd, addr);
  876. do {
  877. next = pud_addr_end(addr, end);
  878. if (pud_none_or_clear_bad(pud))
  879. continue;
  880. ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
  881. if (ret)
  882. return ret;
  883. } while (pud++, addr = next, addr != end);
  884. return 0;
  885. }
  886. static int unuse_vma(struct vm_area_struct *vma,
  887. swp_entry_t entry, struct page *page)
  888. {
  889. pgd_t *pgd;
  890. unsigned long addr, end, next;
  891. int ret;
  892. if (page_anon_vma(page)) {
  893. addr = page_address_in_vma(page, vma);
  894. if (addr == -EFAULT)
  895. return 0;
  896. else
  897. end = addr + PAGE_SIZE;
  898. } else {
  899. addr = vma->vm_start;
  900. end = vma->vm_end;
  901. }
  902. pgd = pgd_offset(vma->vm_mm, addr);
  903. do {
  904. next = pgd_addr_end(addr, end);
  905. if (pgd_none_or_clear_bad(pgd))
  906. continue;
  907. ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
  908. if (ret)
  909. return ret;
  910. } while (pgd++, addr = next, addr != end);
  911. return 0;
  912. }
  913. static int unuse_mm(struct mm_struct *mm,
  914. swp_entry_t entry, struct page *page)
  915. {
  916. struct vm_area_struct *vma;
  917. int ret = 0;
  918. if (!down_read_trylock(&mm->mmap_sem)) {
  919. /*
  920. * Activate page so shrink_inactive_list is unlikely to unmap
  921. * its ptes while lock is dropped, so swapoff can make progress.
  922. */
  923. activate_page(page);
  924. unlock_page(page);
  925. down_read(&mm->mmap_sem);
  926. lock_page(page);
  927. }
  928. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  929. if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
  930. break;
  931. }
  932. up_read(&mm->mmap_sem);
  933. return (ret < 0)? ret: 0;
  934. }
  935. /*
  936. * Scan swap_map from current position to next entry still in use.
  937. * Recycle to start on reaching the end, returning 0 when empty.
  938. */
  939. static unsigned int find_next_to_unuse(struct swap_info_struct *si,
  940. unsigned int prev)
  941. {
  942. unsigned int max = si->max;
  943. unsigned int i = prev;
  944. unsigned char count;
  945. /*
  946. * No need for swap_lock here: we're just looking
  947. * for whether an entry is in use, not modifying it; false
  948. * hits are okay, and sys_swapoff() has already prevented new
  949. * allocations from this area (while holding swap_lock).
  950. */
  951. for (;;) {
  952. if (++i >= max) {
  953. if (!prev) {
  954. i = 0;
  955. break;
  956. }
  957. /*
  958. * No entries in use at top of swap_map,
  959. * loop back to start and recheck there.
  960. */
  961. max = prev + 1;
  962. prev = 0;
  963. i = 1;
  964. }
  965. count = si->swap_map[i];
  966. if (count && swap_count(count) != SWAP_MAP_BAD)
  967. break;
  968. }
  969. return i;
  970. }
  971. /*
  972. * We completely avoid races by reading each swap page in advance,
  973. * and then search for the process using it. All the necessary
  974. * page table adjustments can then be made atomically.
  975. */
  976. static int try_to_unuse(unsigned int type)
  977. {
  978. struct swap_info_struct *si = swap_info[type];
  979. struct mm_struct *start_mm;
  980. unsigned char *swap_map;
  981. unsigned char swcount;
  982. struct page *page;
  983. swp_entry_t entry;
  984. unsigned int i = 0;
  985. int retval = 0;
  986. /*
  987. * When searching mms for an entry, a good strategy is to
  988. * start at the first mm we freed the previous entry from
  989. * (though actually we don't notice whether we or coincidence
  990. * freed the entry). Initialize this start_mm with a hold.
  991. *
  992. * A simpler strategy would be to start at the last mm we
  993. * freed the previous entry from; but that would take less
  994. * advantage of mmlist ordering, which clusters forked mms
  995. * together, child after parent. If we race with dup_mmap(), we
  996. * prefer to resolve parent before child, lest we miss entries
  997. * duplicated after we scanned child: using last mm would invert
  998. * that.
  999. */
  1000. start_mm = &init_mm;
  1001. atomic_inc(&init_mm.mm_users);
  1002. /*
  1003. * Keep on scanning until all entries have gone. Usually,
  1004. * one pass through swap_map is enough, but not necessarily:
  1005. * there are races when an instance of an entry might be missed.
  1006. */
  1007. while ((i = find_next_to_unuse(si, i)) != 0) {
  1008. if (signal_pending(current)) {
  1009. retval = -EINTR;
  1010. break;
  1011. }
  1012. /*
  1013. * Get a page for the entry, using the existing swap
  1014. * cache page if there is one. Otherwise, get a clean
  1015. * page and read the swap into it.
  1016. */
  1017. swap_map = &si->swap_map[i];
  1018. entry = swp_entry(type, i);
  1019. page = read_swap_cache_async(entry,
  1020. GFP_HIGHUSER_MOVABLE, NULL, 0);
  1021. if (!page) {
  1022. /*
  1023. * Either swap_duplicate() failed because entry
  1024. * has been freed independently, and will not be
  1025. * reused since sys_swapoff() already disabled
  1026. * allocation from here, or alloc_page() failed.
  1027. */
  1028. if (!*swap_map)
  1029. continue;
  1030. retval = -ENOMEM;
  1031. break;
  1032. }
  1033. /*
  1034. * Don't hold on to start_mm if it looks like exiting.
  1035. */
  1036. if (atomic_read(&start_mm->mm_users) == 1) {
  1037. mmput(start_mm);
  1038. start_mm = &init_mm;
  1039. atomic_inc(&init_mm.mm_users);
  1040. }
  1041. /*
  1042. * Wait for and lock page. When do_swap_page races with
  1043. * try_to_unuse, do_swap_page can handle the fault much
  1044. * faster than try_to_unuse can locate the entry. This
  1045. * apparently redundant "wait_on_page_locked" lets try_to_unuse
  1046. * defer to do_swap_page in such a case - in some tests,
  1047. * do_swap_page and try_to_unuse repeatedly compete.
  1048. */
  1049. wait_on_page_locked(page);
  1050. wait_on_page_writeback(page);
  1051. lock_page(page);
  1052. wait_on_page_writeback(page);
  1053. /*
  1054. * Remove all references to entry.
  1055. */
  1056. swcount = *swap_map;
  1057. if (swap_count(swcount) == SWAP_MAP_SHMEM) {
  1058. retval = shmem_unuse(entry, page);
  1059. /* page has already been unlocked and released */
  1060. if (retval < 0)
  1061. break;
  1062. continue;
  1063. }
  1064. if (swap_count(swcount) && start_mm != &init_mm)
  1065. retval = unuse_mm(start_mm, entry, page);
  1066. if (swap_count(*swap_map)) {
  1067. int set_start_mm = (*swap_map >= swcount);
  1068. struct list_head *p = &start_mm->mmlist;
  1069. struct mm_struct *new_start_mm = start_mm;
  1070. struct mm_struct *prev_mm = start_mm;
  1071. struct mm_struct *mm;
  1072. atomic_inc(&new_start_mm->mm_users);
  1073. atomic_inc(&prev_mm->mm_users);
  1074. spin_lock(&mmlist_lock);
  1075. while (swap_count(*swap_map) && !retval &&
  1076. (p = p->next) != &start_mm->mmlist) {
  1077. mm = list_entry(p, struct mm_struct, mmlist);
  1078. if (!atomic_inc_not_zero(&mm->mm_users))
  1079. continue;
  1080. spin_unlock(&mmlist_lock);
  1081. mmput(prev_mm);
  1082. prev_mm = mm;
  1083. cond_resched();
  1084. swcount = *swap_map;
  1085. if (!swap_count(swcount)) /* any usage ? */
  1086. ;
  1087. else if (mm == &init_mm)
  1088. set_start_mm = 1;
  1089. else
  1090. retval = unuse_mm(mm, entry, page);
  1091. if (set_start_mm && *swap_map < swcount) {
  1092. mmput(new_start_mm);
  1093. atomic_inc(&mm->mm_users);
  1094. new_start_mm = mm;
  1095. set_start_mm = 0;
  1096. }
  1097. spin_lock(&mmlist_lock);
  1098. }
  1099. spin_unlock(&mmlist_lock);
  1100. mmput(prev_mm);
  1101. mmput(start_mm);
  1102. start_mm = new_start_mm;
  1103. }
  1104. if (retval) {
  1105. unlock_page(page);
  1106. page_cache_release(page);
  1107. break;
  1108. }
  1109. /*
  1110. * If a reference remains (rare), we would like to leave
  1111. * the page in the swap cache; but try_to_unmap could
  1112. * then re-duplicate the entry once we drop page lock,
  1113. * so we might loop indefinitely; also, that page could
  1114. * not be swapped out to other storage meanwhile. So:
  1115. * delete from cache even if there's another reference,
  1116. * after ensuring that the data has been saved to disk -
  1117. * since if the reference remains (rarer), it will be
  1118. * read from disk into another page. Splitting into two
  1119. * pages would be incorrect if swap supported "shared
  1120. * private" pages, but they are handled by tmpfs files.
  1121. *
  1122. * Given how unuse_vma() targets one particular offset
  1123. * in an anon_vma, once the anon_vma has been determined,
  1124. * this splitting happens to be just what is needed to
  1125. * handle where KSM pages have been swapped out: re-reading
  1126. * is unnecessarily slow, but we can fix that later on.
  1127. */
  1128. if (swap_count(*swap_map) &&
  1129. PageDirty(page) && PageSwapCache(page)) {
  1130. struct writeback_control wbc = {
  1131. .sync_mode = WB_SYNC_NONE,
  1132. };
  1133. swap_writepage(page, &wbc);
  1134. lock_page(page);
  1135. wait_on_page_writeback(page);
  1136. }
  1137. /*
  1138. * It is conceivable that a racing task removed this page from
  1139. * swap cache just before we acquired the page lock at the top,
  1140. * or while we dropped it in unuse_mm(). The page might even
  1141. * be back in swap cache on another swap area: that we must not
  1142. * delete, since it may not have been written out to swap yet.
  1143. */
  1144. if (PageSwapCache(page) &&
  1145. likely(page_private(page) == entry.val))
  1146. delete_from_swap_cache(page);
  1147. /*
  1148. * So we could skip searching mms once swap count went
  1149. * to 1, we did not mark any present ptes as dirty: must
  1150. * mark page dirty so shrink_page_list will preserve it.
  1151. */
  1152. SetPageDirty(page);
  1153. unlock_page(page);
  1154. page_cache_release(page);
  1155. /*
  1156. * Make sure that we aren't completely killing
  1157. * interactive performance.
  1158. */
  1159. cond_resched();
  1160. }
  1161. mmput(start_mm);
  1162. return retval;
  1163. }
  1164. /*
  1165. * After a successful try_to_unuse, if no swap is now in use, we know
  1166. * we can empty the mmlist. swap_lock must be held on entry and exit.
  1167. * Note that mmlist_lock nests inside swap_lock, and an mm must be
  1168. * added to the mmlist just after page_duplicate - before would be racy.
  1169. */
  1170. static void drain_mmlist(void)
  1171. {
  1172. struct list_head *p, *next;
  1173. unsigned int type;
  1174. for (type = 0; type < nr_swapfiles; type++)
  1175. if (swap_info[type]->inuse_pages)
  1176. return;
  1177. spin_lock(&mmlist_lock);
  1178. list_for_each_safe(p, next, &init_mm.mmlist)
  1179. list_del_init(p);
  1180. spin_unlock(&mmlist_lock);
  1181. }
  1182. /*
  1183. * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
  1184. * corresponds to page offset for the specified swap entry.
  1185. * Note that the type of this function is sector_t, but it returns page offset
  1186. * into the bdev, not sector offset.
  1187. */
  1188. static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
  1189. {
  1190. struct swap_info_struct *sis;
  1191. struct swap_extent *start_se;
  1192. struct swap_extent *se;
  1193. pgoff_t offset;
  1194. sis = swap_info[swp_type(entry)];
  1195. *bdev = sis->bdev;
  1196. offset = swp_offset(entry);
  1197. start_se = sis->curr_swap_extent;
  1198. se = start_se;
  1199. for ( ; ; ) {
  1200. struct list_head *lh;
  1201. if (se->start_page <= offset &&
  1202. offset < (se->start_page + se->nr_pages)) {
  1203. return se->start_block + (offset - se->start_page);
  1204. }
  1205. lh = se->list.next;
  1206. se = list_entry(lh, struct swap_extent, list);
  1207. sis->curr_swap_extent = se;
  1208. BUG_ON(se == start_se); /* It *must* be present */
  1209. }
  1210. }
  1211. /*
  1212. * Returns the page offset into bdev for the specified page's swap entry.
  1213. */
  1214. sector_t map_swap_page(struct page *page, struct block_device **bdev)
  1215. {
  1216. swp_entry_t entry;
  1217. entry.val = page_private(page);
  1218. return map_swap_entry(entry, bdev);
  1219. }
  1220. /*
  1221. * Free all of a swapdev's extent information
  1222. */
  1223. static void destroy_swap_extents(struct swap_info_struct *sis)
  1224. {
  1225. while (!list_empty(&sis->first_swap_extent.list)) {
  1226. struct swap_extent *se;
  1227. se = list_entry(sis->first_swap_extent.list.next,
  1228. struct swap_extent, list);
  1229. list_del(&se->list);
  1230. kfree(se);
  1231. }
  1232. }
  1233. /*
  1234. * Add a block range (and the corresponding page range) into this swapdev's
  1235. * extent list. The extent list is kept sorted in page order.
  1236. *
  1237. * This function rather assumes that it is called in ascending page order.
  1238. */
  1239. static int
  1240. add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
  1241. unsigned long nr_pages, sector_t start_block)
  1242. {
  1243. struct swap_extent *se;
  1244. struct swap_extent *new_se;
  1245. struct list_head *lh;
  1246. if (start_page == 0) {
  1247. se = &sis->first_swap_extent;
  1248. sis->curr_swap_extent = se;
  1249. se->start_page = 0;
  1250. se->nr_pages = nr_pages;
  1251. se->start_block = start_block;
  1252. return 1;
  1253. } else {
  1254. lh = sis->first_swap_extent.list.prev; /* Highest extent */
  1255. se = list_entry(lh, struct swap_extent, list);
  1256. BUG_ON(se->start_page + se->nr_pages != start_page);
  1257. if (se->start_block + se->nr_pages == start_block) {
  1258. /* Merge it */
  1259. se->nr_pages += nr_pages;
  1260. return 0;
  1261. }
  1262. }
  1263. /*
  1264. * No merge. Insert a new extent, preserving ordering.
  1265. */
  1266. new_se = kmalloc(sizeof(*se), GFP_KERNEL);
  1267. if (new_se == NULL)
  1268. return -ENOMEM;
  1269. new_se->start_page = start_page;
  1270. new_se->nr_pages = nr_pages;
  1271. new_se->start_block = start_block;
  1272. list_add_tail(&new_se->list, &sis->first_swap_extent.list);
  1273. return 1;
  1274. }
  1275. /*
  1276. * A `swap extent' is a simple thing which maps a contiguous range of pages
  1277. * onto a contiguous range of disk blocks. An ordered list of swap extents
  1278. * is built at swapon time and is then used at swap_writepage/swap_readpage
  1279. * time for locating where on disk a page belongs.
  1280. *
  1281. * If the swapfile is an S_ISBLK block device, a single extent is installed.
  1282. * This is done so that the main operating code can treat S_ISBLK and S_ISREG
  1283. * swap files identically.
  1284. *
  1285. * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
  1286. * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
  1287. * swapfiles are handled *identically* after swapon time.
  1288. *
  1289. * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
  1290. * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
  1291. * some stray blocks are found which do not fall within the PAGE_SIZE alignment
  1292. * requirements, they are simply tossed out - we will never use those blocks
  1293. * for swapping.
  1294. *
  1295. * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
  1296. * prevents root from shooting her foot off by ftruncating an in-use swapfile,
  1297. * which will scribble on the fs.
  1298. *
  1299. * The amount of disk space which a single swap extent represents varies.
  1300. * Typically it is in the 1-4 megabyte range. So we can have hundreds of
  1301. * extents in the list. To avoid much list walking, we cache the previous
  1302. * search location in `curr_swap_extent', and start new searches from there.
  1303. * This is extremely effective. The average number of iterations in
  1304. * map_swap_page() has been measured at about 0.3 per page. - akpm.
  1305. */
  1306. static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
  1307. {
  1308. struct inode *inode;
  1309. unsigned blocks_per_page;
  1310. unsigned long page_no;
  1311. unsigned blkbits;
  1312. sector_t probe_block;
  1313. sector_t last_block;
  1314. sector_t lowest_block = -1;
  1315. sector_t highest_block = 0;
  1316. int nr_extents = 0;
  1317. int ret;
  1318. inode = sis->swap_file->f_mapping->host;
  1319. if (S_ISBLK(inode->i_mode)) {
  1320. ret = add_swap_extent(sis, 0, sis->max, 0);
  1321. *span = sis->pages;
  1322. goto out;
  1323. }
  1324. blkbits = inode->i_blkbits;
  1325. blocks_per_page = PAGE_SIZE >> blkbits;
  1326. /*
  1327. * Map all the blocks into the extent list. This code doesn't try
  1328. * to be very smart.
  1329. */
  1330. probe_block = 0;
  1331. page_no = 0;
  1332. last_block = i_size_read(inode) >> blkbits;
  1333. while ((probe_block + blocks_per_page) <= last_block &&
  1334. page_no < sis->max) {
  1335. unsigned block_in_page;
  1336. sector_t first_block;
  1337. first_block = bmap(inode, probe_block);
  1338. if (first_block == 0)
  1339. goto bad_bmap;
  1340. /*
  1341. * It must be PAGE_SIZE aligned on-disk
  1342. */
  1343. if (first_block & (blocks_per_page - 1)) {
  1344. probe_block++;
  1345. goto reprobe;
  1346. }
  1347. for (block_in_page = 1; block_in_page < blocks_per_page;
  1348. block_in_page++) {
  1349. sector_t block;
  1350. block = bmap(inode, probe_block + block_in_page);
  1351. if (block == 0)
  1352. goto bad_bmap;
  1353. if (block != first_block + block_in_page) {
  1354. /* Discontiguity */
  1355. probe_block++;
  1356. goto reprobe;
  1357. }
  1358. }
  1359. first_block >>= (PAGE_SHIFT - blkbits);
  1360. if (page_no) { /* exclude the header page */
  1361. if (first_block < lowest_block)
  1362. lowest_block = first_block;
  1363. if (first_block > highest_block)
  1364. highest_block = first_block;
  1365. }
  1366. /*
  1367. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  1368. */
  1369. ret = add_swap_extent(sis, page_no, 1, first_block);
  1370. if (ret < 0)
  1371. goto out;
  1372. nr_extents += ret;
  1373. page_no++;
  1374. probe_block += blocks_per_page;
  1375. reprobe:
  1376. continue;
  1377. }
  1378. ret = nr_extents;
  1379. *span = 1 + highest_block - lowest_block;
  1380. if (page_no == 0)
  1381. page_no = 1; /* force Empty message */
  1382. sis->max = page_no;
  1383. sis->pages = page_no - 1;
  1384. sis->highest_bit = page_no - 1;
  1385. out:
  1386. return ret;
  1387. bad_bmap:
  1388. printk(KERN_ERR "swapon: swapfile has holes\n");
  1389. ret = -EINVAL;
  1390. goto out;
  1391. }
  1392. SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
  1393. {
  1394. struct swap_info_struct *p = NULL;
  1395. unsigned char *swap_map;
  1396. struct file *swap_file, *victim;
  1397. struct address_space *mapping;
  1398. struct inode *inode;
  1399. char *pathname;
  1400. int i, type, prev;
  1401. int err;
  1402. if (!capable(CAP_SYS_ADMIN))
  1403. return -EPERM;
  1404. pathname = getname(specialfile);
  1405. err = PTR_ERR(pathname);
  1406. if (IS_ERR(pathname))
  1407. goto out;
  1408. victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
  1409. putname(pathname);
  1410. err = PTR_ERR(victim);
  1411. if (IS_ERR(victim))
  1412. goto out;
  1413. mapping = victim->f_mapping;
  1414. prev = -1;
  1415. spin_lock(&swap_lock);
  1416. for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
  1417. p = swap_info[type];
  1418. if (p->flags & SWP_WRITEOK) {
  1419. if (p->swap_file->f_mapping == mapping)
  1420. break;
  1421. }
  1422. prev = type;
  1423. }
  1424. if (type < 0) {
  1425. err = -EINVAL;
  1426. spin_unlock(&swap_lock);
  1427. goto out_dput;
  1428. }
  1429. if (!security_vm_enough_memory(p->pages))
  1430. vm_unacct_memory(p->pages);
  1431. else {
  1432. err = -ENOMEM;
  1433. spin_unlock(&swap_lock);
  1434. goto out_dput;
  1435. }
  1436. if (prev < 0)
  1437. swap_list.head = p->next;
  1438. else
  1439. swap_info[prev]->next = p->next;
  1440. if (type == swap_list.next) {
  1441. /* just pick something that's safe... */
  1442. swap_list.next = swap_list.head;
  1443. }
  1444. if (p->prio < 0) {
  1445. for (i = p->next; i >= 0; i = swap_info[i]->next)
  1446. swap_info[i]->prio = p->prio--;
  1447. least_priority++;
  1448. }
  1449. nr_swap_pages -= p->pages;
  1450. total_swap_pages -= p->pages;
  1451. p->flags &= ~SWP_WRITEOK;
  1452. spin_unlock(&swap_lock);
  1453. current->flags |= PF_OOM_ORIGIN;
  1454. err = try_to_unuse(type);
  1455. current->flags &= ~PF_OOM_ORIGIN;
  1456. if (err) {
  1457. /* re-insert swap space back into swap_list */
  1458. spin_lock(&swap_lock);
  1459. if (p->prio < 0)
  1460. p->prio = --least_priority;
  1461. prev = -1;
  1462. for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
  1463. if (p->prio >= swap_info[i]->prio)
  1464. break;
  1465. prev = i;
  1466. }
  1467. p->next = i;
  1468. if (prev < 0)
  1469. swap_list.head = swap_list.next = type;
  1470. else
  1471. swap_info[prev]->next = type;
  1472. nr_swap_pages += p->pages;
  1473. total_swap_pages += p->pages;
  1474. p->flags |= SWP_WRITEOK;
  1475. spin_unlock(&swap_lock);
  1476. goto out_dput;
  1477. }
  1478. /* wait for any unplug function to finish */
  1479. down_write(&swap_unplug_sem);
  1480. up_write(&swap_unplug_sem);
  1481. destroy_swap_extents(p);
  1482. if (p->flags & SWP_CONTINUED)
  1483. free_swap_count_continuations(p);
  1484. mutex_lock(&swapon_mutex);
  1485. spin_lock(&swap_lock);
  1486. drain_mmlist();
  1487. /* wait for anyone still in scan_swap_map */
  1488. p->highest_bit = 0; /* cuts scans short */
  1489. while (p->flags >= SWP_SCANNING) {
  1490. spin_unlock(&swap_lock);
  1491. schedule_timeout_uninterruptible(1);
  1492. spin_lock(&swap_lock);
  1493. }
  1494. swap_file = p->swap_file;
  1495. p->swap_file = NULL;
  1496. p->max = 0;
  1497. swap_map = p->swap_map;
  1498. p->swap_map = NULL;
  1499. p->flags = 0;
  1500. spin_unlock(&swap_lock);
  1501. mutex_unlock(&swapon_mutex);
  1502. vfree(swap_map);
  1503. /* Destroy swap account informatin */
  1504. swap_cgroup_swapoff(type);
  1505. inode = mapping->host;
  1506. if (S_ISBLK(inode->i_mode)) {
  1507. struct block_device *bdev = I_BDEV(inode);
  1508. set_blocksize(bdev, p->old_block_size);
  1509. blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1510. } else {
  1511. mutex_lock(&inode->i_mutex);
  1512. inode->i_flags &= ~S_SWAPFILE;
  1513. mutex_unlock(&inode->i_mutex);
  1514. }
  1515. filp_close(swap_file, NULL);
  1516. err = 0;
  1517. atomic_inc(&proc_poll_event);
  1518. wake_up_interruptible(&proc_poll_wait);
  1519. out_dput:
  1520. filp_close(victim, NULL);
  1521. out:
  1522. return err;
  1523. }
  1524. #ifdef CONFIG_PROC_FS
  1525. struct proc_swaps {
  1526. struct seq_file seq;
  1527. int event;
  1528. };
  1529. static unsigned swaps_poll(struct file *file, poll_table *wait)
  1530. {
  1531. struct proc_swaps *s = file->private_data;
  1532. poll_wait(file, &proc_poll_wait, wait);
  1533. if (s->event != atomic_read(&proc_poll_event)) {
  1534. s->event = atomic_read(&proc_poll_event);
  1535. return POLLIN | POLLRDNORM | POLLERR | POLLPRI;
  1536. }
  1537. return POLLIN | POLLRDNORM;
  1538. }
  1539. /* iterator */
  1540. static void *swap_start(struct seq_file *swap, loff_t *pos)
  1541. {
  1542. struct swap_info_struct *si;
  1543. int type;
  1544. loff_t l = *pos;
  1545. mutex_lock(&swapon_mutex);
  1546. if (!l)
  1547. return SEQ_START_TOKEN;
  1548. for (type = 0; type < nr_swapfiles; type++) {
  1549. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  1550. si = swap_info[type];
  1551. if (!(si->flags & SWP_USED) || !si->swap_map)
  1552. continue;
  1553. if (!--l)
  1554. return si;
  1555. }
  1556. return NULL;
  1557. }
  1558. static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
  1559. {
  1560. struct swap_info_struct *si = v;
  1561. int type;
  1562. if (v == SEQ_START_TOKEN)
  1563. type = 0;
  1564. else
  1565. type = si->type + 1;
  1566. for (; type < nr_swapfiles; type++) {
  1567. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  1568. si = swap_info[type];
  1569. if (!(si->flags & SWP_USED) || !si->swap_map)
  1570. continue;
  1571. ++*pos;
  1572. return si;
  1573. }
  1574. return NULL;
  1575. }
  1576. static void swap_stop(struct seq_file *swap, void *v)
  1577. {
  1578. mutex_unlock(&swapon_mutex);
  1579. }
  1580. static int swap_show(struct seq_file *swap, void *v)
  1581. {
  1582. struct swap_info_struct *si = v;
  1583. struct file *file;
  1584. int len;
  1585. if (si == SEQ_START_TOKEN) {
  1586. seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
  1587. return 0;
  1588. }
  1589. file = si->swap_file;
  1590. len = seq_path(swap, &file->f_path, " \t\n\\");
  1591. seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
  1592. len < 40 ? 40 - len : 1, " ",
  1593. S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
  1594. "partition" : "file\t",
  1595. si->pages << (PAGE_SHIFT - 10),
  1596. si->inuse_pages << (PAGE_SHIFT - 10),
  1597. si->prio);
  1598. return 0;
  1599. }
  1600. static const struct seq_operations swaps_op = {
  1601. .start = swap_start,
  1602. .next = swap_next,
  1603. .stop = swap_stop,
  1604. .show = swap_show
  1605. };
  1606. static int swaps_open(struct inode *inode, struct file *file)
  1607. {
  1608. struct proc_swaps *s;
  1609. int ret;
  1610. s = kmalloc(sizeof(struct proc_swaps), GFP_KERNEL);
  1611. if (!s)
  1612. return -ENOMEM;
  1613. file->private_data = s;
  1614. ret = seq_open(file, &swaps_op);
  1615. if (ret) {
  1616. kfree(s);
  1617. return ret;
  1618. }
  1619. s->seq.private = s;
  1620. s->event = atomic_read(&proc_poll_event);
  1621. return ret;
  1622. }
  1623. static const struct file_operations proc_swaps_operations = {
  1624. .open = swaps_open,
  1625. .read = seq_read,
  1626. .llseek = seq_lseek,
  1627. .release = seq_release,
  1628. .poll = swaps_poll,
  1629. };
  1630. static int __init procswaps_init(void)
  1631. {
  1632. proc_create("swaps", 0, NULL, &proc_swaps_operations);
  1633. return 0;
  1634. }
  1635. __initcall(procswaps_init);
  1636. #endif /* CONFIG_PROC_FS */
  1637. #ifdef MAX_SWAPFILES_CHECK
  1638. static int __init max_swapfiles_check(void)
  1639. {
  1640. MAX_SWAPFILES_CHECK();
  1641. return 0;
  1642. }
  1643. late_initcall(max_swapfiles_check);
  1644. #endif
  1645. /*
  1646. * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
  1647. *
  1648. * The swapon system call
  1649. */
  1650. SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
  1651. {
  1652. struct swap_info_struct *p;
  1653. char *name = NULL;
  1654. struct block_device *bdev = NULL;
  1655. struct file *swap_file = NULL;
  1656. struct address_space *mapping;
  1657. unsigned int type;
  1658. int i, prev;
  1659. int error;
  1660. union swap_header *swap_header;
  1661. unsigned int nr_good_pages;
  1662. int nr_extents = 0;
  1663. sector_t span;
  1664. unsigned long maxpages;
  1665. unsigned long swapfilepages;
  1666. unsigned char *swap_map = NULL;
  1667. struct page *page = NULL;
  1668. struct inode *inode = NULL;
  1669. int did_down = 0;
  1670. if (!capable(CAP_SYS_ADMIN))
  1671. return -EPERM;
  1672. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1673. if (!p)
  1674. return -ENOMEM;
  1675. spin_lock(&swap_lock);
  1676. for (type = 0; type < nr_swapfiles; type++) {
  1677. if (!(swap_info[type]->flags & SWP_USED))
  1678. break;
  1679. }
  1680. error = -EPERM;
  1681. if (type >= MAX_SWAPFILES) {
  1682. spin_unlock(&swap_lock);
  1683. kfree(p);
  1684. goto out;
  1685. }
  1686. if (type >= nr_swapfiles) {
  1687. p->type = type;
  1688. swap_info[type] = p;
  1689. /*
  1690. * Write swap_info[type] before nr_swapfiles, in case a
  1691. * racing procfs swap_start() or swap_next() is reading them.
  1692. * (We never shrink nr_swapfiles, we never free this entry.)
  1693. */
  1694. smp_wmb();
  1695. nr_swapfiles++;
  1696. } else {
  1697. kfree(p);
  1698. p = swap_info[type];
  1699. /*
  1700. * Do not memset this entry: a racing procfs swap_next()
  1701. * would be relying on p->type to remain valid.
  1702. */
  1703. }
  1704. INIT_LIST_HEAD(&p->first_swap_extent.list);
  1705. p->flags = SWP_USED;
  1706. p->next = -1;
  1707. spin_unlock(&swap_lock);
  1708. name = getname(specialfile);
  1709. error = PTR_ERR(name);
  1710. if (IS_ERR(name)) {
  1711. name = NULL;
  1712. goto bad_swap_2;
  1713. }
  1714. swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
  1715. error = PTR_ERR(swap_file);
  1716. if (IS_ERR(swap_file)) {
  1717. swap_file = NULL;
  1718. goto bad_swap_2;
  1719. }
  1720. p->swap_file = swap_file;
  1721. mapping = swap_file->f_mapping;
  1722. inode = mapping->host;
  1723. error = -EBUSY;
  1724. for (i = 0; i < nr_swapfiles; i++) {
  1725. struct swap_info_struct *q = swap_info[i];
  1726. if (i == type || !q->swap_file)
  1727. continue;
  1728. if (mapping == q->swap_file->f_mapping)
  1729. goto bad_swap;
  1730. }
  1731. error = -EINVAL;
  1732. if (S_ISBLK(inode->i_mode)) {
  1733. bdev = bdgrab(I_BDEV(inode));
  1734. error = blkdev_get(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL,
  1735. sys_swapon);
  1736. if (error < 0) {
  1737. bdev = NULL;
  1738. error = -EINVAL;
  1739. goto bad_swap;
  1740. }
  1741. p->old_block_size = block_size(bdev);
  1742. error = set_blocksize(bdev, PAGE_SIZE);
  1743. if (error < 0)
  1744. goto bad_swap;
  1745. p->bdev = bdev;
  1746. p->flags |= SWP_BLKDEV;
  1747. } else if (S_ISREG(inode->i_mode)) {
  1748. p->bdev = inode->i_sb->s_bdev;
  1749. mutex_lock(&inode->i_mutex);
  1750. did_down = 1;
  1751. if (IS_SWAPFILE(inode)) {
  1752. error = -EBUSY;
  1753. goto bad_swap;
  1754. }
  1755. } else {
  1756. goto bad_swap;
  1757. }
  1758. swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
  1759. /*
  1760. * Read the swap header.
  1761. */
  1762. if (!mapping->a_ops->readpage) {
  1763. error = -EINVAL;
  1764. goto bad_swap;
  1765. }
  1766. page = read_mapping_page(mapping, 0, swap_file);
  1767. if (IS_ERR(page)) {
  1768. error = PTR_ERR(page);
  1769. goto bad_swap;
  1770. }
  1771. swap_header = kmap(page);
  1772. if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
  1773. printk(KERN_ERR "Unable to find swap-space signature\n");
  1774. error = -EINVAL;
  1775. goto bad_swap;
  1776. }
  1777. /* swap partition endianess hack... */
  1778. if (swab32(swap_header->info.version) == 1) {
  1779. swab32s(&swap_header->info.version);
  1780. swab32s(&swap_header->info.last_page);
  1781. swab32s(&swap_header->info.nr_badpages);
  1782. for (i = 0; i < swap_header->info.nr_badpages; i++)
  1783. swab32s(&swap_header->info.badpages[i]);
  1784. }
  1785. /* Check the swap header's sub-version */
  1786. if (swap_header->info.version != 1) {
  1787. printk(KERN_WARNING
  1788. "Unable to handle swap header version %d\n",
  1789. swap_header->info.version);
  1790. error = -EINVAL;
  1791. goto bad_swap;
  1792. }
  1793. p->lowest_bit = 1;
  1794. p->cluster_next = 1;
  1795. p->cluster_nr = 0;
  1796. /*
  1797. * Find out how many pages are allowed for a single swap
  1798. * device. There are two limiting factors: 1) the number of
  1799. * bits for the swap offset in the swp_entry_t type and
  1800. * 2) the number of bits in the a swap pte as defined by
  1801. * the different architectures. In order to find the
  1802. * largest possible bit mask a swap entry with swap type 0
  1803. * and swap offset ~0UL is created, encoded to a swap pte,
  1804. * decoded to a swp_entry_t again and finally the swap
  1805. * offset is extracted. This will mask all the bits from
  1806. * the initial ~0UL mask that can't be encoded in either
  1807. * the swp_entry_t or the architecture definition of a
  1808. * swap pte.
  1809. */
  1810. maxpages = swp_offset(pte_to_swp_entry(
  1811. swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
  1812. if (maxpages > swap_header->info.last_page) {
  1813. maxpages = swap_header->info.last_page + 1;
  1814. /* p->max is an unsigned int: don't overflow it */
  1815. if ((unsigned int)maxpages == 0)
  1816. maxpages = UINT_MAX;
  1817. }
  1818. p->highest_bit = maxpages - 1;
  1819. error = -EINVAL;
  1820. if (!maxpages)
  1821. goto bad_swap;
  1822. if (swapfilepages && maxpages > swapfilepages) {
  1823. printk(KERN_WARNING
  1824. "Swap area shorter than signature indicates\n");
  1825. goto bad_swap;
  1826. }
  1827. if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
  1828. goto bad_swap;
  1829. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  1830. goto bad_swap;
  1831. /* OK, set up the swap map and apply the bad block list */
  1832. swap_map = vmalloc(maxpages);
  1833. if (!swap_map) {
  1834. error = -ENOMEM;
  1835. goto bad_swap;
  1836. }
  1837. memset(swap_map, 0, maxpages);
  1838. nr_good_pages = maxpages - 1; /* omit header page */
  1839. for (i = 0; i < swap_header->info.nr_badpages; i++) {
  1840. unsigned int page_nr = swap_header->info.badpages[i];
  1841. if (page_nr == 0 || page_nr > swap_header->info.last_page) {
  1842. error = -EINVAL;
  1843. goto bad_swap;
  1844. }
  1845. if (page_nr < maxpages) {
  1846. swap_map[page_nr] = SWAP_MAP_BAD;
  1847. nr_good_pages--;
  1848. }
  1849. }
  1850. error = swap_cgroup_swapon(type, maxpages);
  1851. if (error)
  1852. goto bad_swap;
  1853. if (nr_good_pages) {
  1854. swap_map[0] = SWAP_MAP_BAD;
  1855. p->max = maxpages;
  1856. p->pages = nr_good_pages;
  1857. nr_extents = setup_swap_extents(p, &span);
  1858. if (nr_extents < 0) {
  1859. error = nr_extents;
  1860. goto bad_swap;
  1861. }
  1862. nr_good_pages = p->pages;
  1863. }
  1864. if (!nr_good_pages) {
  1865. printk(KERN_WARNING "Empty swap-file\n");
  1866. error = -EINVAL;
  1867. goto bad_swap;
  1868. }
  1869. if (p->bdev) {
  1870. if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
  1871. p->flags |= SWP_SOLIDSTATE;
  1872. p->cluster_next = 1 + (random32() % p->highest_bit);
  1873. }
  1874. if (discard_swap(p) == 0 && (swap_flags & SWAP_FLAG_DISCARD))
  1875. p->flags |= SWP_DISCARDABLE;
  1876. }
  1877. mutex_lock(&swapon_mutex);
  1878. spin_lock(&swap_lock);
  1879. if (swap_flags & SWAP_FLAG_PREFER)
  1880. p->prio =
  1881. (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
  1882. else
  1883. p->prio = --least_priority;
  1884. p->swap_map = swap_map;
  1885. p->flags |= SWP_WRITEOK;
  1886. nr_swap_pages += nr_good_pages;
  1887. total_swap_pages += nr_good_pages;
  1888. printk(KERN_INFO "Adding %uk swap on %s. "
  1889. "Priority:%d extents:%d across:%lluk %s%s\n",
  1890. nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
  1891. nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
  1892. (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
  1893. (p->flags & SWP_DISCARDABLE) ? "D" : "");
  1894. /* insert swap space into swap_list: */
  1895. prev = -1;
  1896. for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
  1897. if (p->prio >= swap_info[i]->prio)
  1898. break;
  1899. prev = i;
  1900. }
  1901. p->next = i;
  1902. if (prev < 0)
  1903. swap_list.head = swap_list.next = type;
  1904. else
  1905. swap_info[prev]->next = type;
  1906. spin_unlock(&swap_lock);
  1907. mutex_unlock(&swapon_mutex);
  1908. atomic_inc(&proc_poll_event);
  1909. wake_up_interruptible(&proc_poll_wait);
  1910. error = 0;
  1911. goto out;
  1912. bad_swap:
  1913. if (bdev) {
  1914. set_blocksize(bdev, p->old_block_size);
  1915. blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1916. }
  1917. destroy_swap_extents(p);
  1918. swap_cgroup_swapoff(type);
  1919. bad_swap_2:
  1920. spin_lock(&swap_lock);
  1921. p->swap_file = NULL;
  1922. p->flags = 0;
  1923. spin_unlock(&swap_lock);
  1924. vfree(swap_map);
  1925. if (swap_file)
  1926. filp_close(swap_file, NULL);
  1927. out:
  1928. if (page && !IS_ERR(page)) {
  1929. kunmap(page);
  1930. page_cache_release(page);
  1931. }
  1932. if (name)
  1933. putname(name);
  1934. if (did_down) {
  1935. if (!error)
  1936. inode->i_flags |= S_SWAPFILE;
  1937. mutex_unlock(&inode->i_mutex);
  1938. }
  1939. return error;
  1940. }
  1941. void si_swapinfo(struct sysinfo *val)
  1942. {
  1943. unsigned int type;
  1944. unsigned long nr_to_be_unused = 0;
  1945. spin_lock(&swap_lock);
  1946. for (type = 0; type < nr_swapfiles; type++) {
  1947. struct swap_info_struct *si = swap_info[type];
  1948. if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
  1949. nr_to_be_unused += si->inuse_pages;
  1950. }
  1951. val->freeswap = nr_swap_pages + nr_to_be_unused;
  1952. val->totalswap = total_swap_pages + nr_to_be_unused;
  1953. spin_unlock(&swap_lock);
  1954. }
  1955. /*
  1956. * Verify that a swap entry is valid and increment its swap map count.
  1957. *
  1958. * Returns error code in following case.
  1959. * - success -> 0
  1960. * - swp_entry is invalid -> EINVAL
  1961. * - swp_entry is migration entry -> EINVAL
  1962. * - swap-cache reference is requested but there is already one. -> EEXIST
  1963. * - swap-cache reference is requested but the entry is not used. -> ENOENT
  1964. * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
  1965. */
  1966. static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
  1967. {
  1968. struct swap_info_struct *p;
  1969. unsigned long offset, type;
  1970. unsigned char count;
  1971. unsigned char has_cache;
  1972. int err = -EINVAL;
  1973. if (non_swap_entry(entry))
  1974. goto out;
  1975. type = swp_type(entry);
  1976. if (type >= nr_swapfiles)
  1977. goto bad_file;
  1978. p = swap_info[type];
  1979. offset = swp_offset(entry);
  1980. spin_lock(&swap_lock);
  1981. if (unlikely(offset >= p->max))
  1982. goto unlock_out;
  1983. count = p->swap_map[offset];
  1984. has_cache = count & SWAP_HAS_CACHE;
  1985. count &= ~SWAP_HAS_CACHE;
  1986. err = 0;
  1987. if (usage == SWAP_HAS_CACHE) {
  1988. /* set SWAP_HAS_CACHE if there is no cache and entry is used */
  1989. if (!has_cache && count)
  1990. has_cache = SWAP_HAS_CACHE;
  1991. else if (has_cache) /* someone else added cache */
  1992. err = -EEXIST;
  1993. else /* no users remaining */
  1994. err = -ENOENT;
  1995. } else if (count || has_cache) {
  1996. if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
  1997. count += usage;
  1998. else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
  1999. err = -EINVAL;
  2000. else if (swap_count_continued(p, offset, count))
  2001. count = COUNT_CONTINUED;
  2002. else
  2003. err = -ENOMEM;
  2004. } else
  2005. err = -ENOENT; /* unused swap entry */
  2006. p->swap_map[offset] = count | has_cache;
  2007. unlock_out:
  2008. spin_unlock(&swap_lock);
  2009. out:
  2010. return err;
  2011. bad_file:
  2012. printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
  2013. goto out;
  2014. }
  2015. /*
  2016. * Help swapoff by noting that swap entry belongs to shmem/tmpfs
  2017. * (in which case its reference count is never incremented).
  2018. */
  2019. void swap_shmem_alloc(swp_entry_t entry)
  2020. {
  2021. __swap_duplicate(entry, SWAP_MAP_SHMEM);
  2022. }
  2023. /*
  2024. * Increase reference count of swap entry by 1.
  2025. * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
  2026. * but could not be atomically allocated. Returns 0, just as if it succeeded,
  2027. * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
  2028. * might occur if a page table entry has got corrupted.
  2029. */
  2030. int swap_duplicate(swp_entry_t entry)
  2031. {
  2032. int err = 0;
  2033. while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
  2034. err = add_swap_count_continuation(entry, GFP_ATOMIC);
  2035. return err;
  2036. }
  2037. /*
  2038. * @entry: swap entry for which we allocate swap cache.
  2039. *
  2040. * Called when allocating swap cache for existing swap entry,
  2041. * This can return error codes. Returns 0 at success.
  2042. * -EBUSY means there is a swap cache.
  2043. * Note: return code is different from swap_duplicate().
  2044. */
  2045. int swapcache_prepare(swp_entry_t entry)
  2046. {
  2047. return __swap_duplicate(entry, SWAP_HAS_CACHE);
  2048. }
  2049. /*
  2050. * swap_lock prevents swap_map being freed. Don't grab an extra
  2051. * reference on the swaphandle, it doesn't matter if it becomes unused.
  2052. */
  2053. int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
  2054. {
  2055. struct swap_info_struct *si;
  2056. int our_page_cluster = page_cluster;
  2057. pgoff_t target, toff;
  2058. pgoff_t base, end;
  2059. int nr_pages = 0;
  2060. if (!our_page_cluster) /* no readahead */
  2061. return 0;
  2062. si = swap_info[swp_type(entry)];
  2063. target = swp_offset(entry);
  2064. base = (target >> our_page_cluster) << our_page_cluster;
  2065. end = base + (1 << our_page_cluster);
  2066. if (!base) /* first page is swap header */
  2067. base++;
  2068. spin_lock(&swap_lock);
  2069. if (end > si->max) /* don't go beyond end of map */
  2070. end = si->max;
  2071. /* Count contiguous allocated slots above our target */
  2072. for (toff = target; ++toff < end; nr_pages++) {
  2073. /* Don't read in free or bad pages */
  2074. if (!si->swap_map[toff])
  2075. break;
  2076. if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
  2077. break;
  2078. }
  2079. /* Count contiguous allocated slots below our target */
  2080. for (toff = target; --toff >= base; nr_pages++) {
  2081. /* Don't read in free or bad pages */
  2082. if (!si->swap_map[toff])
  2083. break;
  2084. if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
  2085. break;
  2086. }
  2087. spin_unlock(&swap_lock);
  2088. /*
  2089. * Indicate starting offset, and return number of pages to get:
  2090. * if only 1, say 0, since there's then no readahead to be done.
  2091. */
  2092. *offset = ++toff;
  2093. return nr_pages? ++nr_pages: 0;
  2094. }
  2095. /*
  2096. * add_swap_count_continuation - called when a swap count is duplicated
  2097. * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
  2098. * page of the original vmalloc'ed swap_map, to hold the continuation count
  2099. * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called
  2100. * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
  2101. *
  2102. * These continuation pages are seldom referenced: the common paths all work
  2103. * on the original swap_map, only referring to a continuation page when the
  2104. * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
  2105. *
  2106. * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
  2107. * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
  2108. * can be called after dropping locks.
  2109. */
  2110. int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
  2111. {
  2112. struct swap_info_struct *si;
  2113. struct page *head;
  2114. struct page *page;
  2115. struct page *list_page;
  2116. pgoff_t offset;
  2117. unsigned char count;
  2118. /*
  2119. * When debugging, it's easier to use __GFP_ZERO here; but it's better
  2120. * for latency not to zero a page while GFP_ATOMIC and holding locks.
  2121. */
  2122. page = alloc_page(gfp_mask | __GFP_HIGHMEM);
  2123. si = swap_info_get(entry);
  2124. if (!si) {
  2125. /*
  2126. * An acceptable race has occurred since the failing
  2127. * __swap_duplicate(): the swap entry has been freed,
  2128. * perhaps even the whole swap_map cleared for swapoff.
  2129. */
  2130. goto outer;
  2131. }
  2132. offset = swp_offset(entry);
  2133. count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
  2134. if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
  2135. /*
  2136. * The higher the swap count, the more likely it is that tasks
  2137. * will race to add swap count continuation: we need to avoid
  2138. * over-provisioning.
  2139. */
  2140. goto out;
  2141. }
  2142. if (!page) {
  2143. spin_unlock(&swap_lock);
  2144. return -ENOMEM;
  2145. }
  2146. /*
  2147. * We are fortunate that although vmalloc_to_page uses pte_offset_map,
  2148. * no architecture is using highmem pages for kernel pagetables: so it
  2149. * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
  2150. */
  2151. head = vmalloc_to_page(si->swap_map + offset);
  2152. offset &= ~PAGE_MASK;
  2153. /*
  2154. * Page allocation does not initialize the page's lru field,
  2155. * but it does always reset its private field.
  2156. */
  2157. if (!page_private(head)) {
  2158. BUG_ON(count & COUNT_CONTINUED);
  2159. INIT_LIST_HEAD(&head->lru);
  2160. set_page_private(head, SWP_CONTINUED);
  2161. si->flags |= SWP_CONTINUED;
  2162. }
  2163. list_for_each_entry(list_page, &head->lru, lru) {
  2164. unsigned char *map;
  2165. /*
  2166. * If the previous map said no continuation, but we've found
  2167. * a continuation page, free our allocation and use this one.
  2168. */
  2169. if (!(count & COUNT_CONTINUED))
  2170. goto out;
  2171. map = kmap_atomic(list_page, KM_USER0) + offset;
  2172. count = *map;
  2173. kunmap_atomic(map, KM_USER0);
  2174. /*
  2175. * If this continuation count now has some space in it,
  2176. * free our allocation and use this one.
  2177. */
  2178. if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
  2179. goto out;
  2180. }
  2181. list_add_tail(&page->lru, &head->lru);
  2182. page = NULL; /* now it's attached, don't free it */
  2183. out:
  2184. spin_unlock(&swap_lock);
  2185. outer:
  2186. if (page)
  2187. __free_page(page);
  2188. return 0;
  2189. }
  2190. /*
  2191. * swap_count_continued - when the original swap_map count is incremented
  2192. * from SWAP_MAP_MAX, check if there is already a continuation page to carry
  2193. * into, carry if so, or else fail until a new continuation page is allocated;
  2194. * when the original swap_map count is decremented from 0 with continuation,
  2195. * borrow from the continuation and report whether it still holds more.
  2196. * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
  2197. */
  2198. static bool swap_count_continued(struct swap_info_struct *si,
  2199. pgoff_t offset, unsigned char count)
  2200. {
  2201. struct page *head;
  2202. struct page *page;
  2203. unsigned char *map;
  2204. head = vmalloc_to_page(si->swap_map + offset);
  2205. if (page_private(head) != SWP_CONTINUED) {
  2206. BUG_ON(count & COUNT_CONTINUED);
  2207. return false; /* need to add count continuation */
  2208. }
  2209. offset &= ~PAGE_MASK;
  2210. page = list_entry(head->lru.next, struct page, lru);
  2211. map = kmap_atomic(page, KM_USER0) + offset;
  2212. if (count == SWAP_MAP_MAX) /* initial increment from swap_map */
  2213. goto init_map; /* jump over SWAP_CONT_MAX checks */
  2214. if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
  2215. /*
  2216. * Think of how you add 1 to 999
  2217. */
  2218. while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
  2219. kunmap_atomic(map, KM_USER0);
  2220. page = list_entry(page->lru.next, struct page, lru);
  2221. BUG_ON(page == head);
  2222. map = kmap_atomic(page, KM_USER0) + offset;
  2223. }
  2224. if (*map == SWAP_CONT_MAX) {
  2225. kunmap_atomic(map, KM_USER0);
  2226. page = list_entry(page->lru.next, struct page, lru);
  2227. if (page == head)
  2228. return false; /* add count continuation */
  2229. map = kmap_atomic(page, KM_USER0) + offset;
  2230. init_map: *map = 0; /* we didn't zero the page */
  2231. }
  2232. *map += 1;
  2233. kunmap_atomic(map, KM_USER0);
  2234. page = list_entry(page->lru.prev, struct page, lru);
  2235. while (page != head) {
  2236. map = kmap_atomic(page, KM_USER0) + offset;
  2237. *map = COUNT_CONTINUED;
  2238. kunmap_atomic(map, KM_USER0);
  2239. page = list_entry(page->lru.prev, struct page, lru);
  2240. }
  2241. return true; /* incremented */
  2242. } else { /* decrementing */
  2243. /*
  2244. * Think of how you subtract 1 from 1000
  2245. */
  2246. BUG_ON(count != COUNT_CONTINUED);
  2247. while (*map == COUNT_CONTINUED) {
  2248. kunmap_atomic(map, KM_USER0);
  2249. page = list_entry(page->lru.next, struct page, lru);
  2250. BUG_ON(page == head);
  2251. map = kmap_atomic(page, KM_USER0) + offset;
  2252. }
  2253. BUG_ON(*map == 0);
  2254. *map -= 1;
  2255. if (*map == 0)
  2256. count = 0;
  2257. kunmap_atomic(map, KM_USER0);
  2258. page = list_entry(page->lru.prev, struct page, lru);
  2259. while (page != head) {
  2260. map = kmap_atomic(page, KM_USER0) + offset;
  2261. *map = SWAP_CONT_MAX | count;
  2262. count = COUNT_CONTINUED;
  2263. kunmap_atomic(map, KM_USER0);
  2264. page = list_entry(page->lru.prev, struct page, lru);
  2265. }
  2266. return count == COUNT_CONTINUED;
  2267. }
  2268. }
  2269. /*
  2270. * free_swap_count_continuations - swapoff free all the continuation pages
  2271. * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
  2272. */
  2273. static void free_swap_count_continuations(struct swap_info_struct *si)
  2274. {
  2275. pgoff_t offset;
  2276. for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
  2277. struct page *head;
  2278. head = vmalloc_to_page(si->swap_map + offset);
  2279. if (page_private(head)) {
  2280. struct list_head *this, *next;
  2281. list_for_each_safe(this, next, &head->lru) {
  2282. struct page *page;
  2283. page = list_entry(this, struct page, lru);
  2284. list_del(this);
  2285. __free_page(page);
  2286. }
  2287. }
  2288. }
  2289. }