swapfile.c 64 KB

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