swapfile.c 45 KB

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