swapfile.c 45 KB

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