swapfile.c 45 KB

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