swapfile.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515
  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. /*
  611. * Once hibernation has begun to create its image of memory,
  612. * there's a danger that one of the calls to try_to_free_swap()
  613. * - most probably a call from __try_to_reclaim_swap() while
  614. * hibernation is allocating its own swap pages for the image,
  615. * but conceivably even a call from memory reclaim - will free
  616. * the swap from a page which has already been recorded in the
  617. * image as a clean swapcache page, and then reuse its swap for
  618. * another page of the image. On waking from hibernation, the
  619. * original page might be freed under memory pressure, then
  620. * later read back in from swap, now with the wrong data.
  621. *
  622. * Hibernation clears bits from gfp_allowed_mask to prevent
  623. * memory reclaim from writing to disk, so check that here.
  624. */
  625. if (!(gfp_allowed_mask & __GFP_IO))
  626. return 0;
  627. delete_from_swap_cache(page);
  628. SetPageDirty(page);
  629. return 1;
  630. }
  631. /*
  632. * Free the swap entry like above, but also try to
  633. * free the page cache entry if it is the last user.
  634. */
  635. int free_swap_and_cache(swp_entry_t entry)
  636. {
  637. struct swap_info_struct *p;
  638. struct page *page = NULL;
  639. if (non_swap_entry(entry))
  640. return 1;
  641. p = swap_info_get(entry);
  642. if (p) {
  643. if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) {
  644. page = find_get_page(&swapper_space, entry.val);
  645. if (page && !trylock_page(page)) {
  646. page_cache_release(page);
  647. page = NULL;
  648. }
  649. }
  650. spin_unlock(&swap_lock);
  651. }
  652. if (page) {
  653. /*
  654. * Not mapped elsewhere, or swap space full? Free it!
  655. * Also recheck PageSwapCache now page is locked (above).
  656. */
  657. if (PageSwapCache(page) && !PageWriteback(page) &&
  658. (!page_mapped(page) || vm_swap_full())) {
  659. delete_from_swap_cache(page);
  660. SetPageDirty(page);
  661. }
  662. unlock_page(page);
  663. page_cache_release(page);
  664. }
  665. return p != NULL;
  666. }
  667. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  668. /**
  669. * mem_cgroup_count_swap_user - count the user of a swap entry
  670. * @ent: the swap entry to be checked
  671. * @pagep: the pointer for the swap cache page of the entry to be stored
  672. *
  673. * Returns the number of the user of the swap entry. The number is valid only
  674. * for swaps of anonymous pages.
  675. * If the entry is found on swap cache, the page is stored to pagep with
  676. * refcount of it being incremented.
  677. */
  678. int mem_cgroup_count_swap_user(swp_entry_t ent, struct page **pagep)
  679. {
  680. struct page *page;
  681. struct swap_info_struct *p;
  682. int count = 0;
  683. page = find_get_page(&swapper_space, ent.val);
  684. if (page)
  685. count += page_mapcount(page);
  686. p = swap_info_get(ent);
  687. if (p) {
  688. count += swap_count(p->swap_map[swp_offset(ent)]);
  689. spin_unlock(&swap_lock);
  690. }
  691. *pagep = page;
  692. return count;
  693. }
  694. #endif
  695. #ifdef CONFIG_HIBERNATION
  696. /*
  697. * Find the swap type that corresponds to given device (if any).
  698. *
  699. * @offset - number of the PAGE_SIZE-sized block of the device, starting
  700. * from 0, in which the swap header is expected to be located.
  701. *
  702. * This is needed for the suspend to disk (aka swsusp).
  703. */
  704. int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
  705. {
  706. struct block_device *bdev = NULL;
  707. int type;
  708. if (device)
  709. bdev = bdget(device);
  710. spin_lock(&swap_lock);
  711. for (type = 0; type < nr_swapfiles; type++) {
  712. struct swap_info_struct *sis = swap_info[type];
  713. if (!(sis->flags & SWP_WRITEOK))
  714. continue;
  715. if (!bdev) {
  716. if (bdev_p)
  717. *bdev_p = bdgrab(sis->bdev);
  718. spin_unlock(&swap_lock);
  719. return type;
  720. }
  721. if (bdev == sis->bdev) {
  722. struct swap_extent *se = &sis->first_swap_extent;
  723. if (se->start_block == offset) {
  724. if (bdev_p)
  725. *bdev_p = bdgrab(sis->bdev);
  726. spin_unlock(&swap_lock);
  727. bdput(bdev);
  728. return type;
  729. }
  730. }
  731. }
  732. spin_unlock(&swap_lock);
  733. if (bdev)
  734. bdput(bdev);
  735. return -ENODEV;
  736. }
  737. /*
  738. * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
  739. * corresponding to given index in swap_info (swap type).
  740. */
  741. sector_t swapdev_block(int type, pgoff_t offset)
  742. {
  743. struct block_device *bdev;
  744. if ((unsigned int)type >= nr_swapfiles)
  745. return 0;
  746. if (!(swap_info[type]->flags & SWP_WRITEOK))
  747. return 0;
  748. return map_swap_entry(swp_entry(type, offset), &bdev);
  749. }
  750. /*
  751. * Return either the total number of swap pages of given type, or the number
  752. * of free pages of that type (depending on @free)
  753. *
  754. * This is needed for software suspend
  755. */
  756. unsigned int count_swap_pages(int type, int free)
  757. {
  758. unsigned int n = 0;
  759. spin_lock(&swap_lock);
  760. if ((unsigned int)type < nr_swapfiles) {
  761. struct swap_info_struct *sis = swap_info[type];
  762. if (sis->flags & SWP_WRITEOK) {
  763. n = sis->pages;
  764. if (free)
  765. n -= sis->inuse_pages;
  766. }
  767. }
  768. spin_unlock(&swap_lock);
  769. return n;
  770. }
  771. #endif /* CONFIG_HIBERNATION */
  772. /*
  773. * No need to decide whether this PTE shares the swap entry with others,
  774. * just let do_wp_page work it out if a write is requested later - to
  775. * force COW, vm_page_prot omits write permission from any private vma.
  776. */
  777. static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
  778. unsigned long addr, swp_entry_t entry, struct page *page)
  779. {
  780. struct mem_cgroup *ptr = NULL;
  781. spinlock_t *ptl;
  782. pte_t *pte;
  783. int ret = 1;
  784. if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
  785. ret = -ENOMEM;
  786. goto out_nolock;
  787. }
  788. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  789. if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
  790. if (ret > 0)
  791. mem_cgroup_cancel_charge_swapin(ptr);
  792. ret = 0;
  793. goto out;
  794. }
  795. dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
  796. inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
  797. get_page(page);
  798. set_pte_at(vma->vm_mm, addr, pte,
  799. pte_mkold(mk_pte(page, vma->vm_page_prot)));
  800. page_add_anon_rmap(page, vma, addr);
  801. mem_cgroup_commit_charge_swapin(page, ptr);
  802. swap_free(entry);
  803. /*
  804. * Move the page to the active list so it is not
  805. * immediately swapped out again after swapon.
  806. */
  807. activate_page(page);
  808. out:
  809. pte_unmap_unlock(pte, ptl);
  810. out_nolock:
  811. return ret;
  812. }
  813. static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  814. unsigned long addr, unsigned long end,
  815. swp_entry_t entry, struct page *page)
  816. {
  817. pte_t swp_pte = swp_entry_to_pte(entry);
  818. pte_t *pte;
  819. int ret = 0;
  820. /*
  821. * We don't actually need pte lock while scanning for swp_pte: since
  822. * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
  823. * page table while we're scanning; though it could get zapped, and on
  824. * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
  825. * of unmatched parts which look like swp_pte, so unuse_pte must
  826. * recheck under pte lock. Scanning without pte lock lets it be
  827. * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
  828. */
  829. pte = pte_offset_map(pmd, addr);
  830. do {
  831. /*
  832. * swapoff spends a _lot_ of time in this loop!
  833. * Test inline before going to call unuse_pte.
  834. */
  835. if (unlikely(pte_same(*pte, swp_pte))) {
  836. pte_unmap(pte);
  837. ret = unuse_pte(vma, pmd, addr, entry, page);
  838. if (ret)
  839. goto out;
  840. pte = pte_offset_map(pmd, addr);
  841. }
  842. } while (pte++, addr += PAGE_SIZE, addr != end);
  843. pte_unmap(pte - 1);
  844. out:
  845. return ret;
  846. }
  847. static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  848. unsigned long addr, unsigned long end,
  849. swp_entry_t entry, struct page *page)
  850. {
  851. pmd_t *pmd;
  852. unsigned long next;
  853. int ret;
  854. pmd = pmd_offset(pud, addr);
  855. do {
  856. next = pmd_addr_end(addr, end);
  857. if (pmd_none_or_clear_bad(pmd))
  858. continue;
  859. ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
  860. if (ret)
  861. return ret;
  862. } while (pmd++, addr = next, addr != end);
  863. return 0;
  864. }
  865. static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  866. unsigned long addr, unsigned long end,
  867. swp_entry_t entry, struct page *page)
  868. {
  869. pud_t *pud;
  870. unsigned long next;
  871. int ret;
  872. pud = pud_offset(pgd, addr);
  873. do {
  874. next = pud_addr_end(addr, end);
  875. if (pud_none_or_clear_bad(pud))
  876. continue;
  877. ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
  878. if (ret)
  879. return ret;
  880. } while (pud++, addr = next, addr != end);
  881. return 0;
  882. }
  883. static int unuse_vma(struct vm_area_struct *vma,
  884. swp_entry_t entry, struct page *page)
  885. {
  886. pgd_t *pgd;
  887. unsigned long addr, end, next;
  888. int ret;
  889. if (page_anon_vma(page)) {
  890. addr = page_address_in_vma(page, vma);
  891. if (addr == -EFAULT)
  892. return 0;
  893. else
  894. end = addr + PAGE_SIZE;
  895. } else {
  896. addr = vma->vm_start;
  897. end = vma->vm_end;
  898. }
  899. pgd = pgd_offset(vma->vm_mm, addr);
  900. do {
  901. next = pgd_addr_end(addr, end);
  902. if (pgd_none_or_clear_bad(pgd))
  903. continue;
  904. ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
  905. if (ret)
  906. return ret;
  907. } while (pgd++, addr = next, addr != end);
  908. return 0;
  909. }
  910. static int unuse_mm(struct mm_struct *mm,
  911. swp_entry_t entry, struct page *page)
  912. {
  913. struct vm_area_struct *vma;
  914. int ret = 0;
  915. if (!down_read_trylock(&mm->mmap_sem)) {
  916. /*
  917. * Activate page so shrink_inactive_list is unlikely to unmap
  918. * its ptes while lock is dropped, so swapoff can make progress.
  919. */
  920. activate_page(page);
  921. unlock_page(page);
  922. down_read(&mm->mmap_sem);
  923. lock_page(page);
  924. }
  925. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  926. if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
  927. break;
  928. }
  929. up_read(&mm->mmap_sem);
  930. return (ret < 0)? ret: 0;
  931. }
  932. /*
  933. * Scan swap_map from current position to next entry still in use.
  934. * Recycle to start on reaching the end, returning 0 when empty.
  935. */
  936. static unsigned int find_next_to_unuse(struct swap_info_struct *si,
  937. unsigned int prev)
  938. {
  939. unsigned int max = si->max;
  940. unsigned int i = prev;
  941. unsigned char count;
  942. /*
  943. * No need for swap_lock here: we're just looking
  944. * for whether an entry is in use, not modifying it; false
  945. * hits are okay, and sys_swapoff() has already prevented new
  946. * allocations from this area (while holding swap_lock).
  947. */
  948. for (;;) {
  949. if (++i >= max) {
  950. if (!prev) {
  951. i = 0;
  952. break;
  953. }
  954. /*
  955. * No entries in use at top of swap_map,
  956. * loop back to start and recheck there.
  957. */
  958. max = prev + 1;
  959. prev = 0;
  960. i = 1;
  961. }
  962. count = si->swap_map[i];
  963. if (count && swap_count(count) != SWAP_MAP_BAD)
  964. break;
  965. }
  966. return i;
  967. }
  968. /*
  969. * We completely avoid races by reading each swap page in advance,
  970. * and then search for the process using it. All the necessary
  971. * page table adjustments can then be made atomically.
  972. */
  973. static int try_to_unuse(unsigned int type)
  974. {
  975. struct swap_info_struct *si = swap_info[type];
  976. struct mm_struct *start_mm;
  977. unsigned char *swap_map;
  978. unsigned char swcount;
  979. struct page *page;
  980. swp_entry_t entry;
  981. unsigned int i = 0;
  982. int retval = 0;
  983. /*
  984. * When searching mms for an entry, a good strategy is to
  985. * start at the first mm we freed the previous entry from
  986. * (though actually we don't notice whether we or coincidence
  987. * freed the entry). Initialize this start_mm with a hold.
  988. *
  989. * A simpler strategy would be to start at the last mm we
  990. * freed the previous entry from; but that would take less
  991. * advantage of mmlist ordering, which clusters forked mms
  992. * together, child after parent. If we race with dup_mmap(), we
  993. * prefer to resolve parent before child, lest we miss entries
  994. * duplicated after we scanned child: using last mm would invert
  995. * that.
  996. */
  997. start_mm = &init_mm;
  998. atomic_inc(&init_mm.mm_users);
  999. /*
  1000. * Keep on scanning until all entries have gone. Usually,
  1001. * one pass through swap_map is enough, but not necessarily:
  1002. * there are races when an instance of an entry might be missed.
  1003. */
  1004. while ((i = find_next_to_unuse(si, i)) != 0) {
  1005. if (signal_pending(current)) {
  1006. retval = -EINTR;
  1007. break;
  1008. }
  1009. /*
  1010. * Get a page for the entry, using the existing swap
  1011. * cache page if there is one. Otherwise, get a clean
  1012. * page and read the swap into it.
  1013. */
  1014. swap_map = &si->swap_map[i];
  1015. entry = swp_entry(type, i);
  1016. page = read_swap_cache_async(entry,
  1017. GFP_HIGHUSER_MOVABLE, NULL, 0);
  1018. if (!page) {
  1019. /*
  1020. * Either swap_duplicate() failed because entry
  1021. * has been freed independently, and will not be
  1022. * reused since sys_swapoff() already disabled
  1023. * allocation from here, or alloc_page() failed.
  1024. */
  1025. if (!*swap_map)
  1026. continue;
  1027. retval = -ENOMEM;
  1028. break;
  1029. }
  1030. /*
  1031. * Don't hold on to start_mm if it looks like exiting.
  1032. */
  1033. if (atomic_read(&start_mm->mm_users) == 1) {
  1034. mmput(start_mm);
  1035. start_mm = &init_mm;
  1036. atomic_inc(&init_mm.mm_users);
  1037. }
  1038. /*
  1039. * Wait for and lock page. When do_swap_page races with
  1040. * try_to_unuse, do_swap_page can handle the fault much
  1041. * faster than try_to_unuse can locate the entry. This
  1042. * apparently redundant "wait_on_page_locked" lets try_to_unuse
  1043. * defer to do_swap_page in such a case - in some tests,
  1044. * do_swap_page and try_to_unuse repeatedly compete.
  1045. */
  1046. wait_on_page_locked(page);
  1047. wait_on_page_writeback(page);
  1048. lock_page(page);
  1049. wait_on_page_writeback(page);
  1050. /*
  1051. * Remove all references to entry.
  1052. */
  1053. swcount = *swap_map;
  1054. if (swap_count(swcount) == SWAP_MAP_SHMEM) {
  1055. retval = shmem_unuse(entry, page);
  1056. /* page has already been unlocked and released */
  1057. if (retval < 0)
  1058. break;
  1059. continue;
  1060. }
  1061. if (swap_count(swcount) && start_mm != &init_mm)
  1062. retval = unuse_mm(start_mm, entry, page);
  1063. if (swap_count(*swap_map)) {
  1064. int set_start_mm = (*swap_map >= swcount);
  1065. struct list_head *p = &start_mm->mmlist;
  1066. struct mm_struct *new_start_mm = start_mm;
  1067. struct mm_struct *prev_mm = start_mm;
  1068. struct mm_struct *mm;
  1069. atomic_inc(&new_start_mm->mm_users);
  1070. atomic_inc(&prev_mm->mm_users);
  1071. spin_lock(&mmlist_lock);
  1072. while (swap_count(*swap_map) && !retval &&
  1073. (p = p->next) != &start_mm->mmlist) {
  1074. mm = list_entry(p, struct mm_struct, mmlist);
  1075. if (!atomic_inc_not_zero(&mm->mm_users))
  1076. continue;
  1077. spin_unlock(&mmlist_lock);
  1078. mmput(prev_mm);
  1079. prev_mm = mm;
  1080. cond_resched();
  1081. swcount = *swap_map;
  1082. if (!swap_count(swcount)) /* any usage ? */
  1083. ;
  1084. else if (mm == &init_mm)
  1085. set_start_mm = 1;
  1086. else
  1087. retval = unuse_mm(mm, entry, page);
  1088. if (set_start_mm && *swap_map < swcount) {
  1089. mmput(new_start_mm);
  1090. atomic_inc(&mm->mm_users);
  1091. new_start_mm = mm;
  1092. set_start_mm = 0;
  1093. }
  1094. spin_lock(&mmlist_lock);
  1095. }
  1096. spin_unlock(&mmlist_lock);
  1097. mmput(prev_mm);
  1098. mmput(start_mm);
  1099. start_mm = new_start_mm;
  1100. }
  1101. if (retval) {
  1102. unlock_page(page);
  1103. page_cache_release(page);
  1104. break;
  1105. }
  1106. /*
  1107. * If a reference remains (rare), we would like to leave
  1108. * the page in the swap cache; but try_to_unmap could
  1109. * then re-duplicate the entry once we drop page lock,
  1110. * so we might loop indefinitely; also, that page could
  1111. * not be swapped out to other storage meanwhile. So:
  1112. * delete from cache even if there's another reference,
  1113. * after ensuring that the data has been saved to disk -
  1114. * since if the reference remains (rarer), it will be
  1115. * read from disk into another page. Splitting into two
  1116. * pages would be incorrect if swap supported "shared
  1117. * private" pages, but they are handled by tmpfs files.
  1118. *
  1119. * Given how unuse_vma() targets one particular offset
  1120. * in an anon_vma, once the anon_vma has been determined,
  1121. * this splitting happens to be just what is needed to
  1122. * handle where KSM pages have been swapped out: re-reading
  1123. * is unnecessarily slow, but we can fix that later on.
  1124. */
  1125. if (swap_count(*swap_map) &&
  1126. PageDirty(page) && PageSwapCache(page)) {
  1127. struct writeback_control wbc = {
  1128. .sync_mode = WB_SYNC_NONE,
  1129. };
  1130. swap_writepage(page, &wbc);
  1131. lock_page(page);
  1132. wait_on_page_writeback(page);
  1133. }
  1134. /*
  1135. * It is conceivable that a racing task removed this page from
  1136. * swap cache just before we acquired the page lock at the top,
  1137. * or while we dropped it in unuse_mm(). The page might even
  1138. * be back in swap cache on another swap area: that we must not
  1139. * delete, since it may not have been written out to swap yet.
  1140. */
  1141. if (PageSwapCache(page) &&
  1142. likely(page_private(page) == entry.val))
  1143. delete_from_swap_cache(page);
  1144. /*
  1145. * So we could skip searching mms once swap count went
  1146. * to 1, we did not mark any present ptes as dirty: must
  1147. * mark page dirty so shrink_page_list will preserve it.
  1148. */
  1149. SetPageDirty(page);
  1150. unlock_page(page);
  1151. page_cache_release(page);
  1152. /*
  1153. * Make sure that we aren't completely killing
  1154. * interactive performance.
  1155. */
  1156. cond_resched();
  1157. }
  1158. mmput(start_mm);
  1159. return retval;
  1160. }
  1161. /*
  1162. * After a successful try_to_unuse, if no swap is now in use, we know
  1163. * we can empty the mmlist. swap_lock must be held on entry and exit.
  1164. * Note that mmlist_lock nests inside swap_lock, and an mm must be
  1165. * added to the mmlist just after page_duplicate - before would be racy.
  1166. */
  1167. static void drain_mmlist(void)
  1168. {
  1169. struct list_head *p, *next;
  1170. unsigned int type;
  1171. for (type = 0; type < nr_swapfiles; type++)
  1172. if (swap_info[type]->inuse_pages)
  1173. return;
  1174. spin_lock(&mmlist_lock);
  1175. list_for_each_safe(p, next, &init_mm.mmlist)
  1176. list_del_init(p);
  1177. spin_unlock(&mmlist_lock);
  1178. }
  1179. /*
  1180. * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
  1181. * corresponds to page offset for the specified swap entry.
  1182. * Note that the type of this function is sector_t, but it returns page offset
  1183. * into the bdev, not sector offset.
  1184. */
  1185. static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
  1186. {
  1187. struct swap_info_struct *sis;
  1188. struct swap_extent *start_se;
  1189. struct swap_extent *se;
  1190. pgoff_t offset;
  1191. sis = swap_info[swp_type(entry)];
  1192. *bdev = sis->bdev;
  1193. offset = swp_offset(entry);
  1194. start_se = sis->curr_swap_extent;
  1195. se = start_se;
  1196. for ( ; ; ) {
  1197. struct list_head *lh;
  1198. if (se->start_page <= offset &&
  1199. offset < (se->start_page + se->nr_pages)) {
  1200. return se->start_block + (offset - se->start_page);
  1201. }
  1202. lh = se->list.next;
  1203. se = list_entry(lh, struct swap_extent, list);
  1204. sis->curr_swap_extent = se;
  1205. BUG_ON(se == start_se); /* It *must* be present */
  1206. }
  1207. }
  1208. /*
  1209. * Returns the page offset into bdev for the specified page's swap entry.
  1210. */
  1211. sector_t map_swap_page(struct page *page, struct block_device **bdev)
  1212. {
  1213. swp_entry_t entry;
  1214. entry.val = page_private(page);
  1215. return map_swap_entry(entry, bdev);
  1216. }
  1217. /*
  1218. * Free all of a swapdev's extent information
  1219. */
  1220. static void destroy_swap_extents(struct swap_info_struct *sis)
  1221. {
  1222. while (!list_empty(&sis->first_swap_extent.list)) {
  1223. struct swap_extent *se;
  1224. se = list_entry(sis->first_swap_extent.list.next,
  1225. struct swap_extent, list);
  1226. list_del(&se->list);
  1227. kfree(se);
  1228. }
  1229. }
  1230. /*
  1231. * Add a block range (and the corresponding page range) into this swapdev's
  1232. * extent list. The extent list is kept sorted in page order.
  1233. *
  1234. * This function rather assumes that it is called in ascending page order.
  1235. */
  1236. static int
  1237. add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
  1238. unsigned long nr_pages, sector_t start_block)
  1239. {
  1240. struct swap_extent *se;
  1241. struct swap_extent *new_se;
  1242. struct list_head *lh;
  1243. if (start_page == 0) {
  1244. se = &sis->first_swap_extent;
  1245. sis->curr_swap_extent = se;
  1246. se->start_page = 0;
  1247. se->nr_pages = nr_pages;
  1248. se->start_block = start_block;
  1249. return 1;
  1250. } else {
  1251. lh = sis->first_swap_extent.list.prev; /* Highest extent */
  1252. se = list_entry(lh, struct swap_extent, list);
  1253. BUG_ON(se->start_page + se->nr_pages != start_page);
  1254. if (se->start_block + se->nr_pages == start_block) {
  1255. /* Merge it */
  1256. se->nr_pages += nr_pages;
  1257. return 0;
  1258. }
  1259. }
  1260. /*
  1261. * No merge. Insert a new extent, preserving ordering.
  1262. */
  1263. new_se = kmalloc(sizeof(*se), GFP_KERNEL);
  1264. if (new_se == NULL)
  1265. return -ENOMEM;
  1266. new_se->start_page = start_page;
  1267. new_se->nr_pages = nr_pages;
  1268. new_se->start_block = start_block;
  1269. list_add_tail(&new_se->list, &sis->first_swap_extent.list);
  1270. return 1;
  1271. }
  1272. /*
  1273. * A `swap extent' is a simple thing which maps a contiguous range of pages
  1274. * onto a contiguous range of disk blocks. An ordered list of swap extents
  1275. * is built at swapon time and is then used at swap_writepage/swap_readpage
  1276. * time for locating where on disk a page belongs.
  1277. *
  1278. * If the swapfile is an S_ISBLK block device, a single extent is installed.
  1279. * This is done so that the main operating code can treat S_ISBLK and S_ISREG
  1280. * swap files identically.
  1281. *
  1282. * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
  1283. * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
  1284. * swapfiles are handled *identically* after swapon time.
  1285. *
  1286. * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
  1287. * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
  1288. * some stray blocks are found which do not fall within the PAGE_SIZE alignment
  1289. * requirements, they are simply tossed out - we will never use those blocks
  1290. * for swapping.
  1291. *
  1292. * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
  1293. * prevents root from shooting her foot off by ftruncating an in-use swapfile,
  1294. * which will scribble on the fs.
  1295. *
  1296. * The amount of disk space which a single swap extent represents varies.
  1297. * Typically it is in the 1-4 megabyte range. So we can have hundreds of
  1298. * extents in the list. To avoid much list walking, we cache the previous
  1299. * search location in `curr_swap_extent', and start new searches from there.
  1300. * This is extremely effective. The average number of iterations in
  1301. * map_swap_page() has been measured at about 0.3 per page. - akpm.
  1302. */
  1303. static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
  1304. {
  1305. struct inode *inode;
  1306. unsigned blocks_per_page;
  1307. unsigned long page_no;
  1308. unsigned blkbits;
  1309. sector_t probe_block;
  1310. sector_t last_block;
  1311. sector_t lowest_block = -1;
  1312. sector_t highest_block = 0;
  1313. int nr_extents = 0;
  1314. int ret;
  1315. inode = sis->swap_file->f_mapping->host;
  1316. if (S_ISBLK(inode->i_mode)) {
  1317. ret = add_swap_extent(sis, 0, sis->max, 0);
  1318. *span = sis->pages;
  1319. goto out;
  1320. }
  1321. blkbits = inode->i_blkbits;
  1322. blocks_per_page = PAGE_SIZE >> blkbits;
  1323. /*
  1324. * Map all the blocks into the extent list. This code doesn't try
  1325. * to be very smart.
  1326. */
  1327. probe_block = 0;
  1328. page_no = 0;
  1329. last_block = i_size_read(inode) >> blkbits;
  1330. while ((probe_block + blocks_per_page) <= last_block &&
  1331. page_no < sis->max) {
  1332. unsigned block_in_page;
  1333. sector_t first_block;
  1334. first_block = bmap(inode, probe_block);
  1335. if (first_block == 0)
  1336. goto bad_bmap;
  1337. /*
  1338. * It must be PAGE_SIZE aligned on-disk
  1339. */
  1340. if (first_block & (blocks_per_page - 1)) {
  1341. probe_block++;
  1342. goto reprobe;
  1343. }
  1344. for (block_in_page = 1; block_in_page < blocks_per_page;
  1345. block_in_page++) {
  1346. sector_t block;
  1347. block = bmap(inode, probe_block + block_in_page);
  1348. if (block == 0)
  1349. goto bad_bmap;
  1350. if (block != first_block + block_in_page) {
  1351. /* Discontiguity */
  1352. probe_block++;
  1353. goto reprobe;
  1354. }
  1355. }
  1356. first_block >>= (PAGE_SHIFT - blkbits);
  1357. if (page_no) { /* exclude the header page */
  1358. if (first_block < lowest_block)
  1359. lowest_block = first_block;
  1360. if (first_block > highest_block)
  1361. highest_block = first_block;
  1362. }
  1363. /*
  1364. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  1365. */
  1366. ret = add_swap_extent(sis, page_no, 1, first_block);
  1367. if (ret < 0)
  1368. goto out;
  1369. nr_extents += ret;
  1370. page_no++;
  1371. probe_block += blocks_per_page;
  1372. reprobe:
  1373. continue;
  1374. }
  1375. ret = nr_extents;
  1376. *span = 1 + highest_block - lowest_block;
  1377. if (page_no == 0)
  1378. page_no = 1; /* force Empty message */
  1379. sis->max = page_no;
  1380. sis->pages = page_no - 1;
  1381. sis->highest_bit = page_no - 1;
  1382. out:
  1383. return ret;
  1384. bad_bmap:
  1385. printk(KERN_ERR "swapon: swapfile has holes\n");
  1386. ret = -EINVAL;
  1387. goto out;
  1388. }
  1389. SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
  1390. {
  1391. struct swap_info_struct *p = NULL;
  1392. unsigned char *swap_map;
  1393. struct file *swap_file, *victim;
  1394. struct address_space *mapping;
  1395. struct inode *inode;
  1396. char *pathname;
  1397. int i, type, prev;
  1398. int err;
  1399. if (!capable(CAP_SYS_ADMIN))
  1400. return -EPERM;
  1401. pathname = getname(specialfile);
  1402. err = PTR_ERR(pathname);
  1403. if (IS_ERR(pathname))
  1404. goto out;
  1405. victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
  1406. putname(pathname);
  1407. err = PTR_ERR(victim);
  1408. if (IS_ERR(victim))
  1409. goto out;
  1410. mapping = victim->f_mapping;
  1411. prev = -1;
  1412. spin_lock(&swap_lock);
  1413. for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
  1414. p = swap_info[type];
  1415. if (p->flags & SWP_WRITEOK) {
  1416. if (p->swap_file->f_mapping == mapping)
  1417. break;
  1418. }
  1419. prev = type;
  1420. }
  1421. if (type < 0) {
  1422. err = -EINVAL;
  1423. spin_unlock(&swap_lock);
  1424. goto out_dput;
  1425. }
  1426. if (!security_vm_enough_memory(p->pages))
  1427. vm_unacct_memory(p->pages);
  1428. else {
  1429. err = -ENOMEM;
  1430. spin_unlock(&swap_lock);
  1431. goto out_dput;
  1432. }
  1433. if (prev < 0)
  1434. swap_list.head = p->next;
  1435. else
  1436. swap_info[prev]->next = p->next;
  1437. if (type == swap_list.next) {
  1438. /* just pick something that's safe... */
  1439. swap_list.next = swap_list.head;
  1440. }
  1441. if (p->prio < 0) {
  1442. for (i = p->next; i >= 0; i = swap_info[i]->next)
  1443. swap_info[i]->prio = p->prio--;
  1444. least_priority++;
  1445. }
  1446. nr_swap_pages -= p->pages;
  1447. total_swap_pages -= p->pages;
  1448. p->flags &= ~SWP_WRITEOK;
  1449. spin_unlock(&swap_lock);
  1450. current->flags |= PF_OOM_ORIGIN;
  1451. err = try_to_unuse(type);
  1452. current->flags &= ~PF_OOM_ORIGIN;
  1453. if (err) {
  1454. /* re-insert swap space back into swap_list */
  1455. spin_lock(&swap_lock);
  1456. if (p->prio < 0)
  1457. p->prio = --least_priority;
  1458. prev = -1;
  1459. for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
  1460. if (p->prio >= swap_info[i]->prio)
  1461. break;
  1462. prev = i;
  1463. }
  1464. p->next = i;
  1465. if (prev < 0)
  1466. swap_list.head = swap_list.next = type;
  1467. else
  1468. swap_info[prev]->next = type;
  1469. nr_swap_pages += p->pages;
  1470. total_swap_pages += p->pages;
  1471. p->flags |= SWP_WRITEOK;
  1472. spin_unlock(&swap_lock);
  1473. goto out_dput;
  1474. }
  1475. /* wait for any unplug function to finish */
  1476. down_write(&swap_unplug_sem);
  1477. up_write(&swap_unplug_sem);
  1478. destroy_swap_extents(p);
  1479. if (p->flags & SWP_CONTINUED)
  1480. free_swap_count_continuations(p);
  1481. mutex_lock(&swapon_mutex);
  1482. spin_lock(&swap_lock);
  1483. drain_mmlist();
  1484. /* wait for anyone still in scan_swap_map */
  1485. p->highest_bit = 0; /* cuts scans short */
  1486. while (p->flags >= SWP_SCANNING) {
  1487. spin_unlock(&swap_lock);
  1488. schedule_timeout_uninterruptible(1);
  1489. spin_lock(&swap_lock);
  1490. }
  1491. swap_file = p->swap_file;
  1492. p->swap_file = NULL;
  1493. p->max = 0;
  1494. swap_map = p->swap_map;
  1495. p->swap_map = NULL;
  1496. p->flags = 0;
  1497. spin_unlock(&swap_lock);
  1498. mutex_unlock(&swapon_mutex);
  1499. vfree(swap_map);
  1500. /* Destroy swap account informatin */
  1501. swap_cgroup_swapoff(type);
  1502. inode = mapping->host;
  1503. if (S_ISBLK(inode->i_mode)) {
  1504. struct block_device *bdev = I_BDEV(inode);
  1505. set_blocksize(bdev, p->old_block_size);
  1506. bd_release(bdev);
  1507. } else {
  1508. mutex_lock(&inode->i_mutex);
  1509. inode->i_flags &= ~S_SWAPFILE;
  1510. mutex_unlock(&inode->i_mutex);
  1511. }
  1512. filp_close(swap_file, NULL);
  1513. err = 0;
  1514. out_dput:
  1515. filp_close(victim, NULL);
  1516. out:
  1517. return err;
  1518. }
  1519. #ifdef CONFIG_PROC_FS
  1520. /* iterator */
  1521. static void *swap_start(struct seq_file *swap, loff_t *pos)
  1522. {
  1523. struct swap_info_struct *si;
  1524. int type;
  1525. loff_t l = *pos;
  1526. mutex_lock(&swapon_mutex);
  1527. if (!l)
  1528. return SEQ_START_TOKEN;
  1529. for (type = 0; type < nr_swapfiles; type++) {
  1530. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  1531. si = swap_info[type];
  1532. if (!(si->flags & SWP_USED) || !si->swap_map)
  1533. continue;
  1534. if (!--l)
  1535. return si;
  1536. }
  1537. return NULL;
  1538. }
  1539. static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
  1540. {
  1541. struct swap_info_struct *si = v;
  1542. int type;
  1543. if (v == SEQ_START_TOKEN)
  1544. type = 0;
  1545. else
  1546. type = si->type + 1;
  1547. for (; type < nr_swapfiles; type++) {
  1548. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  1549. si = swap_info[type];
  1550. if (!(si->flags & SWP_USED) || !si->swap_map)
  1551. continue;
  1552. ++*pos;
  1553. return si;
  1554. }
  1555. return NULL;
  1556. }
  1557. static void swap_stop(struct seq_file *swap, void *v)
  1558. {
  1559. mutex_unlock(&swapon_mutex);
  1560. }
  1561. static int swap_show(struct seq_file *swap, void *v)
  1562. {
  1563. struct swap_info_struct *si = v;
  1564. struct file *file;
  1565. int len;
  1566. if (si == SEQ_START_TOKEN) {
  1567. seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
  1568. return 0;
  1569. }
  1570. file = si->swap_file;
  1571. len = seq_path(swap, &file->f_path, " \t\n\\");
  1572. seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
  1573. len < 40 ? 40 - len : 1, " ",
  1574. S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
  1575. "partition" : "file\t",
  1576. si->pages << (PAGE_SHIFT - 10),
  1577. si->inuse_pages << (PAGE_SHIFT - 10),
  1578. si->prio);
  1579. return 0;
  1580. }
  1581. static const struct seq_operations swaps_op = {
  1582. .start = swap_start,
  1583. .next = swap_next,
  1584. .stop = swap_stop,
  1585. .show = swap_show
  1586. };
  1587. static int swaps_open(struct inode *inode, struct file *file)
  1588. {
  1589. return seq_open(file, &swaps_op);
  1590. }
  1591. static const struct file_operations proc_swaps_operations = {
  1592. .open = swaps_open,
  1593. .read = seq_read,
  1594. .llseek = seq_lseek,
  1595. .release = seq_release,
  1596. };
  1597. static int __init procswaps_init(void)
  1598. {
  1599. proc_create("swaps", 0, NULL, &proc_swaps_operations);
  1600. return 0;
  1601. }
  1602. __initcall(procswaps_init);
  1603. #endif /* CONFIG_PROC_FS */
  1604. #ifdef MAX_SWAPFILES_CHECK
  1605. static int __init max_swapfiles_check(void)
  1606. {
  1607. MAX_SWAPFILES_CHECK();
  1608. return 0;
  1609. }
  1610. late_initcall(max_swapfiles_check);
  1611. #endif
  1612. /*
  1613. * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
  1614. *
  1615. * The swapon system call
  1616. */
  1617. SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
  1618. {
  1619. struct swap_info_struct *p;
  1620. char *name = NULL;
  1621. struct block_device *bdev = NULL;
  1622. struct file *swap_file = NULL;
  1623. struct address_space *mapping;
  1624. unsigned int type;
  1625. int i, prev;
  1626. int error;
  1627. union swap_header *swap_header;
  1628. unsigned int nr_good_pages;
  1629. int nr_extents = 0;
  1630. sector_t span;
  1631. unsigned long maxpages;
  1632. unsigned long swapfilepages;
  1633. unsigned char *swap_map = NULL;
  1634. struct page *page = NULL;
  1635. struct inode *inode = NULL;
  1636. int did_down = 0;
  1637. if (!capable(CAP_SYS_ADMIN))
  1638. return -EPERM;
  1639. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1640. if (!p)
  1641. return -ENOMEM;
  1642. spin_lock(&swap_lock);
  1643. for (type = 0; type < nr_swapfiles; type++) {
  1644. if (!(swap_info[type]->flags & SWP_USED))
  1645. break;
  1646. }
  1647. error = -EPERM;
  1648. if (type >= MAX_SWAPFILES) {
  1649. spin_unlock(&swap_lock);
  1650. kfree(p);
  1651. goto out;
  1652. }
  1653. if (type >= nr_swapfiles) {
  1654. p->type = type;
  1655. swap_info[type] = p;
  1656. /*
  1657. * Write swap_info[type] before nr_swapfiles, in case a
  1658. * racing procfs swap_start() or swap_next() is reading them.
  1659. * (We never shrink nr_swapfiles, we never free this entry.)
  1660. */
  1661. smp_wmb();
  1662. nr_swapfiles++;
  1663. } else {
  1664. kfree(p);
  1665. p = swap_info[type];
  1666. /*
  1667. * Do not memset this entry: a racing procfs swap_next()
  1668. * would be relying on p->type to remain valid.
  1669. */
  1670. }
  1671. INIT_LIST_HEAD(&p->first_swap_extent.list);
  1672. p->flags = SWP_USED;
  1673. p->next = -1;
  1674. spin_unlock(&swap_lock);
  1675. name = getname(specialfile);
  1676. error = PTR_ERR(name);
  1677. if (IS_ERR(name)) {
  1678. name = NULL;
  1679. goto bad_swap_2;
  1680. }
  1681. swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
  1682. error = PTR_ERR(swap_file);
  1683. if (IS_ERR(swap_file)) {
  1684. swap_file = NULL;
  1685. goto bad_swap_2;
  1686. }
  1687. p->swap_file = swap_file;
  1688. mapping = swap_file->f_mapping;
  1689. inode = mapping->host;
  1690. error = -EBUSY;
  1691. for (i = 0; i < nr_swapfiles; i++) {
  1692. struct swap_info_struct *q = swap_info[i];
  1693. if (i == type || !q->swap_file)
  1694. continue;
  1695. if (mapping == q->swap_file->f_mapping)
  1696. goto bad_swap;
  1697. }
  1698. error = -EINVAL;
  1699. if (S_ISBLK(inode->i_mode)) {
  1700. bdev = I_BDEV(inode);
  1701. error = bd_claim(bdev, sys_swapon);
  1702. if (error < 0) {
  1703. bdev = NULL;
  1704. error = -EINVAL;
  1705. goto bad_swap;
  1706. }
  1707. p->old_block_size = block_size(bdev);
  1708. error = set_blocksize(bdev, PAGE_SIZE);
  1709. if (error < 0)
  1710. goto bad_swap;
  1711. p->bdev = bdev;
  1712. p->flags |= SWP_BLKDEV;
  1713. } else if (S_ISREG(inode->i_mode)) {
  1714. p->bdev = inode->i_sb->s_bdev;
  1715. mutex_lock(&inode->i_mutex);
  1716. did_down = 1;
  1717. if (IS_SWAPFILE(inode)) {
  1718. error = -EBUSY;
  1719. goto bad_swap;
  1720. }
  1721. } else {
  1722. goto bad_swap;
  1723. }
  1724. swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
  1725. /*
  1726. * Read the swap header.
  1727. */
  1728. if (!mapping->a_ops->readpage) {
  1729. error = -EINVAL;
  1730. goto bad_swap;
  1731. }
  1732. page = read_mapping_page(mapping, 0, swap_file);
  1733. if (IS_ERR(page)) {
  1734. error = PTR_ERR(page);
  1735. goto bad_swap;
  1736. }
  1737. swap_header = kmap(page);
  1738. if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
  1739. printk(KERN_ERR "Unable to find swap-space signature\n");
  1740. error = -EINVAL;
  1741. goto bad_swap;
  1742. }
  1743. /* swap partition endianess hack... */
  1744. if (swab32(swap_header->info.version) == 1) {
  1745. swab32s(&swap_header->info.version);
  1746. swab32s(&swap_header->info.last_page);
  1747. swab32s(&swap_header->info.nr_badpages);
  1748. for (i = 0; i < swap_header->info.nr_badpages; i++)
  1749. swab32s(&swap_header->info.badpages[i]);
  1750. }
  1751. /* Check the swap header's sub-version */
  1752. if (swap_header->info.version != 1) {
  1753. printk(KERN_WARNING
  1754. "Unable to handle swap header version %d\n",
  1755. swap_header->info.version);
  1756. error = -EINVAL;
  1757. goto bad_swap;
  1758. }
  1759. p->lowest_bit = 1;
  1760. p->cluster_next = 1;
  1761. p->cluster_nr = 0;
  1762. /*
  1763. * Find out how many pages are allowed for a single swap
  1764. * device. There are two limiting factors: 1) the number of
  1765. * bits for the swap offset in the swp_entry_t type and
  1766. * 2) the number of bits in the a swap pte as defined by
  1767. * the different architectures. In order to find the
  1768. * largest possible bit mask a swap entry with swap type 0
  1769. * and swap offset ~0UL is created, encoded to a swap pte,
  1770. * decoded to a swp_entry_t again and finally the swap
  1771. * offset is extracted. This will mask all the bits from
  1772. * the initial ~0UL mask that can't be encoded in either
  1773. * the swp_entry_t or the architecture definition of a
  1774. * swap pte.
  1775. */
  1776. maxpages = swp_offset(pte_to_swp_entry(
  1777. swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
  1778. if (maxpages > swap_header->info.last_page) {
  1779. maxpages = swap_header->info.last_page + 1;
  1780. /* p->max is an unsigned int: don't overflow it */
  1781. if ((unsigned int)maxpages == 0)
  1782. maxpages = UINT_MAX;
  1783. }
  1784. p->highest_bit = maxpages - 1;
  1785. error = -EINVAL;
  1786. if (!maxpages)
  1787. goto bad_swap;
  1788. if (swapfilepages && maxpages > swapfilepages) {
  1789. printk(KERN_WARNING
  1790. "Swap area shorter than signature indicates\n");
  1791. goto bad_swap;
  1792. }
  1793. if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
  1794. goto bad_swap;
  1795. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  1796. goto bad_swap;
  1797. /* OK, set up the swap map and apply the bad block list */
  1798. swap_map = vmalloc(maxpages);
  1799. if (!swap_map) {
  1800. error = -ENOMEM;
  1801. goto bad_swap;
  1802. }
  1803. memset(swap_map, 0, maxpages);
  1804. nr_good_pages = maxpages - 1; /* omit header page */
  1805. for (i = 0; i < swap_header->info.nr_badpages; i++) {
  1806. unsigned int page_nr = swap_header->info.badpages[i];
  1807. if (page_nr == 0 || page_nr > swap_header->info.last_page) {
  1808. error = -EINVAL;
  1809. goto bad_swap;
  1810. }
  1811. if (page_nr < maxpages) {
  1812. swap_map[page_nr] = SWAP_MAP_BAD;
  1813. nr_good_pages--;
  1814. }
  1815. }
  1816. error = swap_cgroup_swapon(type, maxpages);
  1817. if (error)
  1818. goto bad_swap;
  1819. if (nr_good_pages) {
  1820. swap_map[0] = SWAP_MAP_BAD;
  1821. p->max = maxpages;
  1822. p->pages = nr_good_pages;
  1823. nr_extents = setup_swap_extents(p, &span);
  1824. if (nr_extents < 0) {
  1825. error = nr_extents;
  1826. goto bad_swap;
  1827. }
  1828. nr_good_pages = p->pages;
  1829. }
  1830. if (!nr_good_pages) {
  1831. printk(KERN_WARNING "Empty swap-file\n");
  1832. error = -EINVAL;
  1833. goto bad_swap;
  1834. }
  1835. if (p->bdev) {
  1836. if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
  1837. p->flags |= SWP_SOLIDSTATE;
  1838. p->cluster_next = 1 + (random32() % p->highest_bit);
  1839. }
  1840. if (discard_swap(p) == 0)
  1841. p->flags |= SWP_DISCARDABLE;
  1842. }
  1843. mutex_lock(&swapon_mutex);
  1844. spin_lock(&swap_lock);
  1845. if (swap_flags & SWAP_FLAG_PREFER)
  1846. p->prio =
  1847. (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
  1848. else
  1849. p->prio = --least_priority;
  1850. p->swap_map = swap_map;
  1851. p->flags |= SWP_WRITEOK;
  1852. nr_swap_pages += nr_good_pages;
  1853. total_swap_pages += nr_good_pages;
  1854. printk(KERN_INFO "Adding %uk swap on %s. "
  1855. "Priority:%d extents:%d across:%lluk %s%s\n",
  1856. nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
  1857. nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
  1858. (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
  1859. (p->flags & SWP_DISCARDABLE) ? "D" : "");
  1860. /* insert swap space into swap_list: */
  1861. prev = -1;
  1862. for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
  1863. if (p->prio >= swap_info[i]->prio)
  1864. break;
  1865. prev = i;
  1866. }
  1867. p->next = i;
  1868. if (prev < 0)
  1869. swap_list.head = swap_list.next = type;
  1870. else
  1871. swap_info[prev]->next = type;
  1872. spin_unlock(&swap_lock);
  1873. mutex_unlock(&swapon_mutex);
  1874. error = 0;
  1875. goto out;
  1876. bad_swap:
  1877. if (bdev) {
  1878. set_blocksize(bdev, p->old_block_size);
  1879. bd_release(bdev);
  1880. }
  1881. destroy_swap_extents(p);
  1882. swap_cgroup_swapoff(type);
  1883. bad_swap_2:
  1884. spin_lock(&swap_lock);
  1885. p->swap_file = NULL;
  1886. p->flags = 0;
  1887. spin_unlock(&swap_lock);
  1888. vfree(swap_map);
  1889. if (swap_file)
  1890. filp_close(swap_file, NULL);
  1891. out:
  1892. if (page && !IS_ERR(page)) {
  1893. kunmap(page);
  1894. page_cache_release(page);
  1895. }
  1896. if (name)
  1897. putname(name);
  1898. if (did_down) {
  1899. if (!error)
  1900. inode->i_flags |= S_SWAPFILE;
  1901. mutex_unlock(&inode->i_mutex);
  1902. }
  1903. return error;
  1904. }
  1905. void si_swapinfo(struct sysinfo *val)
  1906. {
  1907. unsigned int type;
  1908. unsigned long nr_to_be_unused = 0;
  1909. spin_lock(&swap_lock);
  1910. for (type = 0; type < nr_swapfiles; type++) {
  1911. struct swap_info_struct *si = swap_info[type];
  1912. if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
  1913. nr_to_be_unused += si->inuse_pages;
  1914. }
  1915. val->freeswap = nr_swap_pages + nr_to_be_unused;
  1916. val->totalswap = total_swap_pages + nr_to_be_unused;
  1917. spin_unlock(&swap_lock);
  1918. }
  1919. /*
  1920. * Verify that a swap entry is valid and increment its swap map count.
  1921. *
  1922. * Returns error code in following case.
  1923. * - success -> 0
  1924. * - swp_entry is invalid -> EINVAL
  1925. * - swp_entry is migration entry -> EINVAL
  1926. * - swap-cache reference is requested but there is already one. -> EEXIST
  1927. * - swap-cache reference is requested but the entry is not used. -> ENOENT
  1928. * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
  1929. */
  1930. static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
  1931. {
  1932. struct swap_info_struct *p;
  1933. unsigned long offset, type;
  1934. unsigned char count;
  1935. unsigned char has_cache;
  1936. int err = -EINVAL;
  1937. if (non_swap_entry(entry))
  1938. goto out;
  1939. type = swp_type(entry);
  1940. if (type >= nr_swapfiles)
  1941. goto bad_file;
  1942. p = swap_info[type];
  1943. offset = swp_offset(entry);
  1944. spin_lock(&swap_lock);
  1945. if (unlikely(offset >= p->max))
  1946. goto unlock_out;
  1947. count = p->swap_map[offset];
  1948. has_cache = count & SWAP_HAS_CACHE;
  1949. count &= ~SWAP_HAS_CACHE;
  1950. err = 0;
  1951. if (usage == SWAP_HAS_CACHE) {
  1952. /* set SWAP_HAS_CACHE if there is no cache and entry is used */
  1953. if (!has_cache && count)
  1954. has_cache = SWAP_HAS_CACHE;
  1955. else if (has_cache) /* someone else added cache */
  1956. err = -EEXIST;
  1957. else /* no users remaining */
  1958. err = -ENOENT;
  1959. } else if (count || has_cache) {
  1960. if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
  1961. count += usage;
  1962. else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
  1963. err = -EINVAL;
  1964. else if (swap_count_continued(p, offset, count))
  1965. count = COUNT_CONTINUED;
  1966. else
  1967. err = -ENOMEM;
  1968. } else
  1969. err = -ENOENT; /* unused swap entry */
  1970. p->swap_map[offset] = count | has_cache;
  1971. unlock_out:
  1972. spin_unlock(&swap_lock);
  1973. out:
  1974. return err;
  1975. bad_file:
  1976. printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
  1977. goto out;
  1978. }
  1979. /*
  1980. * Help swapoff by noting that swap entry belongs to shmem/tmpfs
  1981. * (in which case its reference count is never incremented).
  1982. */
  1983. void swap_shmem_alloc(swp_entry_t entry)
  1984. {
  1985. __swap_duplicate(entry, SWAP_MAP_SHMEM);
  1986. }
  1987. /*
  1988. * Increase reference count of swap entry by 1.
  1989. * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
  1990. * but could not be atomically allocated. Returns 0, just as if it succeeded,
  1991. * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
  1992. * might occur if a page table entry has got corrupted.
  1993. */
  1994. int swap_duplicate(swp_entry_t entry)
  1995. {
  1996. int err = 0;
  1997. while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
  1998. err = add_swap_count_continuation(entry, GFP_ATOMIC);
  1999. return err;
  2000. }
  2001. /*
  2002. * @entry: swap entry for which we allocate swap cache.
  2003. *
  2004. * Called when allocating swap cache for existing swap entry,
  2005. * This can return error codes. Returns 0 at success.
  2006. * -EBUSY means there is a swap cache.
  2007. * Note: return code is different from swap_duplicate().
  2008. */
  2009. int swapcache_prepare(swp_entry_t entry)
  2010. {
  2011. return __swap_duplicate(entry, SWAP_HAS_CACHE);
  2012. }
  2013. /*
  2014. * swap_lock prevents swap_map being freed. Don't grab an extra
  2015. * reference on the swaphandle, it doesn't matter if it becomes unused.
  2016. */
  2017. int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
  2018. {
  2019. struct swap_info_struct *si;
  2020. int our_page_cluster = page_cluster;
  2021. pgoff_t target, toff;
  2022. pgoff_t base, end;
  2023. int nr_pages = 0;
  2024. if (!our_page_cluster) /* no readahead */
  2025. return 0;
  2026. si = swap_info[swp_type(entry)];
  2027. target = swp_offset(entry);
  2028. base = (target >> our_page_cluster) << our_page_cluster;
  2029. end = base + (1 << our_page_cluster);
  2030. if (!base) /* first page is swap header */
  2031. base++;
  2032. spin_lock(&swap_lock);
  2033. if (end > si->max) /* don't go beyond end of map */
  2034. end = si->max;
  2035. /* Count contiguous allocated slots above our target */
  2036. for (toff = target; ++toff < end; nr_pages++) {
  2037. /* Don't read in free or bad pages */
  2038. if (!si->swap_map[toff])
  2039. break;
  2040. if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
  2041. break;
  2042. }
  2043. /* Count contiguous allocated slots below our target */
  2044. for (toff = target; --toff >= base; nr_pages++) {
  2045. /* Don't read in free or bad pages */
  2046. if (!si->swap_map[toff])
  2047. break;
  2048. if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
  2049. break;
  2050. }
  2051. spin_unlock(&swap_lock);
  2052. /*
  2053. * Indicate starting offset, and return number of pages to get:
  2054. * if only 1, say 0, since there's then no readahead to be done.
  2055. */
  2056. *offset = ++toff;
  2057. return nr_pages? ++nr_pages: 0;
  2058. }
  2059. /*
  2060. * add_swap_count_continuation - called when a swap count is duplicated
  2061. * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
  2062. * page of the original vmalloc'ed swap_map, to hold the continuation count
  2063. * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called
  2064. * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
  2065. *
  2066. * These continuation pages are seldom referenced: the common paths all work
  2067. * on the original swap_map, only referring to a continuation page when the
  2068. * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
  2069. *
  2070. * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
  2071. * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
  2072. * can be called after dropping locks.
  2073. */
  2074. int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
  2075. {
  2076. struct swap_info_struct *si;
  2077. struct page *head;
  2078. struct page *page;
  2079. struct page *list_page;
  2080. pgoff_t offset;
  2081. unsigned char count;
  2082. /*
  2083. * When debugging, it's easier to use __GFP_ZERO here; but it's better
  2084. * for latency not to zero a page while GFP_ATOMIC and holding locks.
  2085. */
  2086. page = alloc_page(gfp_mask | __GFP_HIGHMEM);
  2087. si = swap_info_get(entry);
  2088. if (!si) {
  2089. /*
  2090. * An acceptable race has occurred since the failing
  2091. * __swap_duplicate(): the swap entry has been freed,
  2092. * perhaps even the whole swap_map cleared for swapoff.
  2093. */
  2094. goto outer;
  2095. }
  2096. offset = swp_offset(entry);
  2097. count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
  2098. if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
  2099. /*
  2100. * The higher the swap count, the more likely it is that tasks
  2101. * will race to add swap count continuation: we need to avoid
  2102. * over-provisioning.
  2103. */
  2104. goto out;
  2105. }
  2106. if (!page) {
  2107. spin_unlock(&swap_lock);
  2108. return -ENOMEM;
  2109. }
  2110. /*
  2111. * We are fortunate that although vmalloc_to_page uses pte_offset_map,
  2112. * no architecture is using highmem pages for kernel pagetables: so it
  2113. * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
  2114. */
  2115. head = vmalloc_to_page(si->swap_map + offset);
  2116. offset &= ~PAGE_MASK;
  2117. /*
  2118. * Page allocation does not initialize the page's lru field,
  2119. * but it does always reset its private field.
  2120. */
  2121. if (!page_private(head)) {
  2122. BUG_ON(count & COUNT_CONTINUED);
  2123. INIT_LIST_HEAD(&head->lru);
  2124. set_page_private(head, SWP_CONTINUED);
  2125. si->flags |= SWP_CONTINUED;
  2126. }
  2127. list_for_each_entry(list_page, &head->lru, lru) {
  2128. unsigned char *map;
  2129. /*
  2130. * If the previous map said no continuation, but we've found
  2131. * a continuation page, free our allocation and use this one.
  2132. */
  2133. if (!(count & COUNT_CONTINUED))
  2134. goto out;
  2135. map = kmap_atomic(list_page, KM_USER0) + offset;
  2136. count = *map;
  2137. kunmap_atomic(map, KM_USER0);
  2138. /*
  2139. * If this continuation count now has some space in it,
  2140. * free our allocation and use this one.
  2141. */
  2142. if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
  2143. goto out;
  2144. }
  2145. list_add_tail(&page->lru, &head->lru);
  2146. page = NULL; /* now it's attached, don't free it */
  2147. out:
  2148. spin_unlock(&swap_lock);
  2149. outer:
  2150. if (page)
  2151. __free_page(page);
  2152. return 0;
  2153. }
  2154. /*
  2155. * swap_count_continued - when the original swap_map count is incremented
  2156. * from SWAP_MAP_MAX, check if there is already a continuation page to carry
  2157. * into, carry if so, or else fail until a new continuation page is allocated;
  2158. * when the original swap_map count is decremented from 0 with continuation,
  2159. * borrow from the continuation and report whether it still holds more.
  2160. * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
  2161. */
  2162. static bool swap_count_continued(struct swap_info_struct *si,
  2163. pgoff_t offset, unsigned char count)
  2164. {
  2165. struct page *head;
  2166. struct page *page;
  2167. unsigned char *map;
  2168. head = vmalloc_to_page(si->swap_map + offset);
  2169. if (page_private(head) != SWP_CONTINUED) {
  2170. BUG_ON(count & COUNT_CONTINUED);
  2171. return false; /* need to add count continuation */
  2172. }
  2173. offset &= ~PAGE_MASK;
  2174. page = list_entry(head->lru.next, struct page, lru);
  2175. map = kmap_atomic(page, KM_USER0) + offset;
  2176. if (count == SWAP_MAP_MAX) /* initial increment from swap_map */
  2177. goto init_map; /* jump over SWAP_CONT_MAX checks */
  2178. if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
  2179. /*
  2180. * Think of how you add 1 to 999
  2181. */
  2182. while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
  2183. kunmap_atomic(map, KM_USER0);
  2184. page = list_entry(page->lru.next, struct page, lru);
  2185. BUG_ON(page == head);
  2186. map = kmap_atomic(page, KM_USER0) + offset;
  2187. }
  2188. if (*map == SWAP_CONT_MAX) {
  2189. kunmap_atomic(map, KM_USER0);
  2190. page = list_entry(page->lru.next, struct page, lru);
  2191. if (page == head)
  2192. return false; /* add count continuation */
  2193. map = kmap_atomic(page, KM_USER0) + offset;
  2194. init_map: *map = 0; /* we didn't zero the page */
  2195. }
  2196. *map += 1;
  2197. kunmap_atomic(map, KM_USER0);
  2198. page = list_entry(page->lru.prev, struct page, lru);
  2199. while (page != head) {
  2200. map = kmap_atomic(page, KM_USER0) + offset;
  2201. *map = COUNT_CONTINUED;
  2202. kunmap_atomic(map, KM_USER0);
  2203. page = list_entry(page->lru.prev, struct page, lru);
  2204. }
  2205. return true; /* incremented */
  2206. } else { /* decrementing */
  2207. /*
  2208. * Think of how you subtract 1 from 1000
  2209. */
  2210. BUG_ON(count != COUNT_CONTINUED);
  2211. while (*map == COUNT_CONTINUED) {
  2212. kunmap_atomic(map, KM_USER0);
  2213. page = list_entry(page->lru.next, struct page, lru);
  2214. BUG_ON(page == head);
  2215. map = kmap_atomic(page, KM_USER0) + offset;
  2216. }
  2217. BUG_ON(*map == 0);
  2218. *map -= 1;
  2219. if (*map == 0)
  2220. count = 0;
  2221. kunmap_atomic(map, KM_USER0);
  2222. page = list_entry(page->lru.prev, struct page, lru);
  2223. while (page != head) {
  2224. map = kmap_atomic(page, KM_USER0) + offset;
  2225. *map = SWAP_CONT_MAX | count;
  2226. count = COUNT_CONTINUED;
  2227. kunmap_atomic(map, KM_USER0);
  2228. page = list_entry(page->lru.prev, struct page, lru);
  2229. }
  2230. return count == COUNT_CONTINUED;
  2231. }
  2232. }
  2233. /*
  2234. * free_swap_count_continuations - swapoff free all the continuation pages
  2235. * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
  2236. */
  2237. static void free_swap_count_continuations(struct swap_info_struct *si)
  2238. {
  2239. pgoff_t offset;
  2240. for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
  2241. struct page *head;
  2242. head = vmalloc_to_page(si->swap_map + offset);
  2243. if (page_private(head)) {
  2244. struct list_head *this, *next;
  2245. list_for_each_safe(this, next, &head->lru) {
  2246. struct page *page;
  2247. page = list_entry(this, struct page, lru);
  2248. list_del(this);
  2249. __free_page(page);
  2250. }
  2251. }
  2252. }
  2253. }