swapfile.c 46 KB

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