swapfile.c 51 KB

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