swapfile.c 56 KB

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