swapfile.c 46 KB

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