swapfile.c 55 KB

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