swapfile.c 66 KB

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