swapfile.c 45 KB

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