swapfile.c 55 KB

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