swapfile.c 45 KB

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