swapfile.c 64 KB

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