swapfile.c 46 KB

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